简体   繁体   中英

Python: Regex to match multiline strings in C code

I'm attempting to match multiline strings in C code via the re module.

I'd like to match strings of the form:

char * theString = "Some string \
                   I want to match.";

I tried the following regex, which does not work:

regex = re.compile(r"\".*\"$", re.MULTILINE)

I thought that it would match the first ", then continue searching the next line until it found a closing ", but this is not the case. Is this because $ requires that there be a " at the end of the line to match? Is there some way to do this using regex?

Use dot all flag.

However, this is the way to parse C strings. (?s)"[^"\\]*(?:\\.[^"\\]*)*"

if it doesn't support (?s) inline modifier, set the modifier in the flags parameter.

re.compile(r'"[^"\\]*(?:\\.[^"\\]*)*"', re.DOTALL)

 (?s)
 "
 [^"\\]*                       # Double quoted text
 (?: \\ . [^"\\]* )*
 "

Ideally, you should add (raw regex) (?<?\\)(::\\\\)* at the beginning,
to make sure the opening double quote is not escaped.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM