今天爱分享给大家带来Python里面search()和match()的区别是什么【面试题详解附代码】,希望能够帮助到大家。
它们两个都在re模块中
·match()函数是在string的开始位置匹配,如果不匹配,则返回None;
·search()会扫描整个string查找匹配;
match()
>>> import re
>>> print(re.match('hello','helloworld').span()) # 开头匹配到
(0, 5)
>>> print(re.match('hello','nicehelloworld').span()) # 开头没有匹配到
Traceback (most recent call last):
File "", line 1, in
print(re.match('hello','nicehelloworld').span())
AttributeError: 'NoneType' object has no attribute 'span'
>>>
search()
>>> print(re.search('a','abc'))
<_sre.SRE_Match object; span=(0, 1), match='a'>
>>> print(re.search('a','bac').span())
(1, 2)
>>>
结论:match() 使用限制更多
原文链接:https://blog.itblood.com/2368.html,转载请注明出处。

![我兄弟的妻子0.12汉化版[PC+安卓/3.00G/更新]My Brother's Wife [v0.12] [欧美SLG/汉化/动态]](/wp-content/uploads/replace/2025/07/19/20c0bed956b0ff44a98520e4ee9d0327.webp)