I want to substitute this until "Their" with "Success!" in python with re.sub.
##### Beispiel:
> **Their
I have tried the following:
text = re.sub(r'##### Beispiel:\n\n > \*\*','Success!', text)
What am I doing wrong?
CodePudding user response:
According to the documentation, backslashes are not handled as special characters when using raw strings (r"..."
)
The solution is to use Python’s raw string notation for regular expression patterns; backslashes are not handled in any special way in a string literal prefixed with 'r'. So r"\n" is a two-character string containing '' and 'n', while "\n" is a one-character string containing a newline. Usually patterns will be expressed in Python code using this raw string notation. Source
You can also play around with the re.MULTILINE
mode or use the whitespace character \s
in case the lines are empty.
CodePudding user response:
I prefer specifying flags
which tell the pattern to include newlines as characters (through the re.DOTALL
option):
print(re.sub(r'.*?beispiel:.*?>[^a-z]*', "success ", content, flags=re.DOTALL|re.IGNORECASE))