re.match versus re.findall

python, regex

Solution

`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:

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

`re.match` will return `None` because the string does not start with `"mail failure"`. `re.findall` though will return a match because the string contains `"mail failure"`.

Problem

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?

Original source