re.match只匹配字符串的开始,如果字符串开始不符合正则表达式,则匹配失败,函数返回None;而re.search匹配整个字符串,直到找到一个匹配。
下面直接上代码:
########################
import re
line = “Cats are smarter than dogs”;
matchObj = re.match(r dogs , line, re.M | re.I)
if matchObj:
print(
“match –> matchObj.group() : “, matchObj.group())
else:
print(
“No match!!”)
matchObj = re.search(r dogs , line, re.M | re.I)
if matchObj:
print(
“search –> searchObj.group() : “, matchObj.group())
else:
print(
“No match!!”)
########################
执行结果如下:
No match!!
search –> searchObj.group() : dogs
________________END______________
© 版权声明
文章版权归作者所有,未经允许请勿转载。如内容涉嫌侵权,请在本页底部进入<联系我们>进行举报投诉!
THE END



















- 最新
- 最热
只看作者