永发信息网

python正则表达式函数match和search的区别详解

答案:1  悬赏:60  手机版
解决时间 2021-01-21 03:33
  • 提问者网友:鐵馬踏冰河
  • 2021-01-20 23:23
python正则表达式函数match和search的区别详解
最佳答案
  • 五星知识达人网友:蓝房子
  • 2021-01-21 00:00
python re文档上有对match VS search的话,摘录如下
Python offers two different primitive operations based on regular expressions:
re.match() checks for a match only at the beginning
of the string, while re.search() checks for a match anywhere in the
string (this is what Perl does by default).
翻译:
python提供了2个基于正则表达式的不同的原始操作。re.match验证只有开头才匹配的字符串对象。而re.search()可以验证在任何位置的字符串(这一项也是perl语言所默认的)
所以区别就是,一个只匹配开头的字符串,一个可以匹配任意地方
举例说明:
re.match("c", "abcdef") # 不匹配,match的返回值是None

re.search("c", "abcdef") # Match
mat = re.match("c", "cdef") # match
print mat.group()#可以打印出匹配的c
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯