I can\'t figure out how to match a string but not if it has a trailing newline character (\\n
), which seems automatically stripped:
import re
p
The documentation says this about the $
character:
Matches the end of the string or just before the newline at the end of the string, and in MULTILINE mode also matches before a newline.
So, without the MULTILINE option, it matches exactly the first two strings you tried: 'foobar'
and 'foobar\n'
, but not 'foobar\n\n'
, because that is not a newline at the end of the string.
On the other hand, if you choose MULTILINE option, it will match the end of any line:
>>> re.match(r'^foobar$', 'foobar\n\n', re.MULTILINE)
<_sre.SRE_Match object; span=(0, 6), match='foobar'>
Of course, this will also match in the following case, which may or may not be what you want:
>>> re.match(r'^foobar$', 'foobar\nanother line\n', re.MULTILINE)
<_sre.SRE_Match object; span=(0, 6), match='foobar'>
In order to NOT match the ending newline, use the negative lookahead as DeepSpace wrote.