I can\'t figure out how to match a string but not if it has a trailing newline character (\\n), which seems automatically stripped:
\\n
import re p
You more likely don't need $ but rather \Z:
$
\Z
>>> print(re.match(r'^foobar\Z', 'foobar\n')) None