re.match与re.findall

Al_*_*Al_ 6 python regex

为什么会这样,re.match返回None对象,而类似的re.findall返回非空结果?

我正在解析电子邮件主题.问题是

subject = "=?UTF-8?B?0JLQsNGI0LUg0YHQvtC+0LHRidC10L3QuNC1INC90LUg0LTQvtGB0YLQsNCy0LvQtdC90L4=?=. Mail failure."
Run Code Online (Sandbox Code Playgroud)

我想知道为什么

re.match("mail failure", subject, re.I) 返回对象的无对象

re.findall("mail failure", subject, re.I) 返回列表中匹配的字符串 ['Mail failure']

我的想法有什么问题?

iCo*_*dez 10

re.match匹配字符串开头的模式. re.findall但是会在字符串中的任何位置搜索模式的出现次数.

如果你有模式"mail failure"和字符串:

subject = "=?UTF-8?B?0JLQsNGI0LUg0YHQvtC+0LHRidC10L3QuNC1INC90LUg0LTQvtGB0YLQsNCy0LvQtdC90L4=?=. Mail failure."
Run Code Online (Sandbox Code Playgroud)

re.match将返回,None因为字符串不是以字符串开头的"mail failure". re.findall虽然会返回匹配,因为字符串包含"mail failure".


tim*_*geb 5

它就在文档中:https : //docs.python.org/2/library/re.html 你想要的是re.search.

请注意,即使在 MULTILINE 模式下, re.match() 也只会匹配字符串的开头而不是每行的开头。