Why is it, re.match
returns the None
object whilst a similar re.findall
returns a non-empty result?
I'm parsing email subjects. The one in question is
subject = "=?UTF-8?B?0JLQsNGI0LUg0YHQvtC+0LHRidC10L3QuNC1INC90LUg0LTQvtGB0YLQsNCy0LvQtdC90L4=?=. Mail failure."
I'm wondering why
re.match("mail failure", subject, re.I)
returns the None object whist the counterpart
re.findall("mail failure", subject, re.I)
returns the matched string in a list ['Mail failure']
What is wrong in my thoughts?
re.match
matches the pattern from the start of the string.re.findall
however searches for occurrences of the pattern anywhere in the string.If you have the pattern
"mail failure"
and the string:re.match
will returnNone
because the string does not start with"mail failure"
.re.findall
though will return a match because the string contains"mail failure"
.It's right here in the docs: https://docs.python.org/2/library/re.html What you want is
re.search
.