141 – Regex matches across newlines

141 – Regex matches across newlines#

The special character . (dot) matches anything in the context of a regular expression, except for the newline character:

import re

pattern = r"<!--.*-->"  # HTML multiline comment.
text = """# Python drops <!--This
is a multiline comment.-->"""
print(re.search(pattern, text))  # None

If you want to use the special character . (dot) for matches that potentially span across multiple lines, you’ll need to use flag re.DOTALL:

print(re.search(pattern, text, flags=re.DOTALL))
# <re.Match object; ...