解释Python正则表达式搜索与匹配
re.match()和re.search()都是Python模块re的方法。
如果re.match()方法出现在字符串的开头,则找到匹配项。例如,在字符串“TPTutorialsPointTP”上调用match()并查找模式“TP”将匹配。
示例
result = re.match(r'TP', 'TP Tutorials Point TP') print result.group(0)
输出结果
TP
re.search()方法与re.match()类似,但它并不限制我们仅在字符串的开头查找匹配项。
例
result = re.search(r'Tutorials', 'TP Tutorials Point TP') print result.group(0)
输出结果
Tutorials
在这里您可以看到,search()方法能够从字符串的任何位置查找模式。