What's the difference between re.DOTALL and re.MULTILINE? [duplicate]
They are quite different. Yes, both affect how newlines are treated, but they switch behaviour for different concepts.
-
re.MULTILINE
affects where^
and$
anchors match.Without the switch,
^
and$
match only at the start and end, respectively, of the whole text. With the switch, they also match just before or after a newline:>>> import re >>> re.search('foo$', 'foo bar') is None # no match True >>> re.search('foo$', 'foo bar', flags=re.MULTILINE) <_sre.SRE_Match object; span=(0, 3), match='foo'>
-
re.DOTALL
affects what the.
pattern can match.Without the switch,
.
matches any character except a newline. With the switch, newlines are matched as well:>>> re.search('foo.', 'foo bar') is None # no match True >>> re.search('foo.', 'foo bar', flags=re.DOTALL) <_sre.SRE_Match object; span=(0, 4), match='foo '>