简体   繁体   中英

how to create regular expression in python

Regular expression for removing all lines between #if X and #endif //#if X Note the C style comment is important and needs to be taken into account

#if X
....
.....
#endif //#if X

The following is not giving desired o/p: So is the re right ?

re.compile("#if.*?#endif //#if X", re.MULTILINE + re.DOTALL)

So far, you have just compiled your regex, you haven't done anything with it yet.

You need to do this:

myregex = re.compile(r"#if.*?#endif //#if X", re.DOTALL)
result = myregex.sub("", subject)

where subject is the string you want to work on (and "" is the replacement string).

You don't need the re.MULTILINE parameter since you're not using the start-/end-of-line anchors at all.

Instead of 're.MULTILINE + re.DOTALL' try 're.MULTILINE | re.DOTALL' , it's a bit field

re.compile(r'#if\s+([A-Z]+)$.+?#endif\s+//\s*#if\s+\1', re.M | re.S)

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