1

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)
1
  • What is the problem ("not the desired o/p" is rather vague)? Commented Mar 28, 2011 at 13:40

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

1 Comment

Although that shouldn't matter here because all the flags are powers of 2, so adding and "oring" them will yield the same result. You're still right, though.
1
re.compile(r'#if\s+([A-Z]+)$.+?#endif\s+//\s*#if\s+\1', re.M | re.S)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.