3

I'm currently looking for the correct regex to replace something like this in Python:

old_string contains:

some text
some text
<!-V
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat
V->
some text

What I've tried to replace the <!-V *** V-> part:

import re
new_string = re.sub(r"<!-V(.*?)V->", '', old_string)

But it seems not correct, maybe anyone can help me with the correct regex.

1 Answer 1

7

By default, . doesn't match a line break character. If you want . to match those too, use the DOTALL flag:

new_string = re.sub(r"<!-V(.*?)V->", '', old_string, flags=re.DOTALL)

There's also the option of replacing . with a character class that matches everything, by combining two complementary character classes:

[\s\S]
Sign up to request clarification or add additional context in comments.

1 Comment

I never even thought of the complementary character class solution; that's a handy trick.

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.