Reputation: 3016
I am trying to match a Regex against a multi-line string, but the match fails after the first line.
These expressions work as expected:
>>> import re
>>> r = re.compile("a")
>>> a = "a"
>>> r.match(a)
<_sre.SRE_Match object; span=(0, 1), match='a'>
>>> a = "a\n"
>>> r.match(a)
<_sre.SRE_Match object; span=(0, 1), match='a'>
>>>
Whilst this expression does not work:
>>> a = "\na"
>>> r.match(a)
>>>
Upvotes: 0
Views: 201
Reputation:
re.match
was designed to match from the first character (the start) of the string. In the first two examples, the match works fine because a
is the first character. In the last example however, the match fails because \n
is the first character.
You need to use re.search
in this case to have Python search for the a
:
>>> import re
>>> r = re.compile("a")
>>> a = "\na"
>>> r.search(a)
<_sre.SRE_Match object; span=(1, 2), match='a'>
>>>
Also, just a note: if you are working with multi-line strings, then you will need to set the dot-all flag to have .
match newlines. This can be done with re.DOTALL
.
Upvotes: 3
Reputation: 26667
Why doesnt match
work?
match
searches the pattern at the start of the string.
How to correct?
use search
instead
>>> import re
>>> pat=re.compile('a')
>>> pat.search('\na')
<_sre.SRE_Match object at 0x7faef636d440>
>>>
Upvotes: 2