Python中re.search和re.match有什么区别?

Python中re.search和re.match有什么区别?

re.match()和re.search()都是Python模块re的方法。

re.match()方法在字符串开头找到匹配项。例如,在字符串“TP Tutorials Point TP”上调用match()并寻找模式“TP”会找到匹配项。

阅读更多:Python 教程

例子

import re
result = re.match(r'TP', 'TP Tutorials Point TP')
print result.group(0)

输出

TP

re.search()方法类似于re.match(),但不限于仅在字符串开头查找匹配项。

例子

import re
result = re.search(r'Tutorials', 'TP Tutorials Point TP')
print result.group(0)

输出

Tutorials

在这里,你可以看到,search()方法能够从字符串的任何位置找到一个匹配项。

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程