Eleno
Eleno

Reputation: 3016

Matching a Regex against a multiline string

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

Answers (2)

user2555451
user2555451

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

nu11p01n73R
nu11p01n73R

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

Related Questions