区别比较 match和search

Posted xingkongzhizhu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了区别比较 match和search相关的知识,希望对你有一定的参考价值。

match() 从第一个字符开始找, 如果第一个字符就不匹配就返回None, 不继续匹配. 用于判断字符串开头或整个字符串是否匹配,速度快。
search() 会整个字符串查找,直到找到一个匹配。
 
import re
print(re.match(‘hello‘, ‘hello world‘).span())
print(re.match(‘hello‘, ‘hihelloworld‘))
print(re.search(‘hello‘, ‘hello world‘).span())
print(re.search(‘hello‘, ‘hihelloworld‘).span())
输出结果为:
(0, 5)
None
(0, 5)
(2, 7)
————————————————
版权声明:本文为CSDN博主「等一个人微笑丶」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_42523181/article/details/81749121

以上是关于区别比较 match和search的主要内容,如果未能解决你的问题,请参考以下文章

Python里面search和match的区别

Python RE模块中search和match的区别

5.Python里面match()和search()的区别?

Lucene、Elasticsearch、Solr区别

python题目-----search()和match()的区别

Python里面search()和match()的区别