In a regular expression, you need to specify that you're in multiline mode:
>>> import re
>>> s = """cat
... dog"""
>>>
>>> re.match(r'cat\ndog',s,re.M)
<_sre.SRE_Match object at 0xcb7c8>
Notice that re
translates the \n
(raw string) into newline. As you indicated in your comments, you don't actually need re.M
for it to match, but it does help with matching $
and ^
more intuitively:
>> re.match(r'^cat\ndog',s).group(0)
'cat\ndog'
>>> re.match(r'^cat$\ndog',s).group(0) #doesn't match
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'group'
>>> re.match(r'^cat$\ndog',s,re.M).group(0) #matches.
'cat\ndog'