Pascal Bajorat
Pascal Bajorat

Reputation: 484

Correct Regex-Syntax for re.sub replacement in Python

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.

Upvotes: 3

Views: 152

Answers (1)

user2357112
user2357112

Reputation: 280778

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]

Upvotes: 7

Related Questions