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 fo开发者_如何学Cllowing 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)
精彩评论