Re.match vs re.findall

Why does this re.matchreturn an object None, while a similar one re.findallreturns a non-empty result?

I am parsing email topics. The one in question

subject = "=?UTF-8?B?0JLQsNGI0LUg0YHQvtC+0LHRidC10L3QuNC1INC90LUg0LTQvtGB0YLQsNCy0LvQtdC90L4=?=. Mail failure."

I wonder why

re.match("mail failure", subject, re.I) returns a None object pointing to an instance

re.findall("mail failure", subject, re.I) returns a matching string in the list ['Mail failure']

What is wrong in my thoughts?

+4
source share
2 answers

re.matchmatches the pattern from the beginning of the line. re.findallHowever, it searches for occurrences of the pattern anywhere in the string.

If you have a pattern "mail failure"and a string:

subject = "=?UTF-8?B?0JLQsNGI0LUg0YHQvtC+0LHRidC10L3QuNC1INC90LUg0LTQvtGB0YLQsNCy0LvQtdC90L4=?=. Mail failure."

re.matchwill return Nonebecause the string does not start with "mail failure". re.findall, although it will return a match because the string contains "mail failure".

+7

, : https://docs.python.org/2/library/re.html re.search.

, MULTILINE re.match() , .

+3

Source: https://habr.com/ru/post/1543233/


All Articles