re.match versus re.findall

2020-02-29 03:20发布

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?

标签: python regex
2条回答
家丑人穷心不美
2楼-- · 2020-02-29 03:36

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".

查看更多
可以哭但决不认输i
3楼-- · 2020-02-29 04:00

It's right here in the docs: https://docs.python.org/2/library/re.html What you want is re.search.

Note that even in MULTILINE mode, re.match() will only match at the beginning of the string and not at the beginning of each line.

查看更多
登录 后发表回答