首页 > 编程语言 > 详细

python正则表达式match,search,find的使用方法

时间:2017-12-11 19:25:28      阅读:235      评论:0      收藏:0      [点我收藏+]

1.使用match()匹配字符串:

match()函数试图从字符串的开始部分对模式进行匹配,

匹配对象的group()方法能够用于显示那个成功的匹配。

>>>import re
>>>m=re.match(foo, fooid).group()
foo
>>>n=re.match(foo, idfooid).group()
AttributeError:NoneType object has no attribute group

2.使用search()函数匹配字符串

search()的工作方式和match()完全一样,不同之处在于search()会用它的字符串参数,

在任意位置对给定的正则表达式模式搜索第一次出现的匹配情况。

如果匹配成功,就会返回一个匹配对象。

>>>import re
>>>m=re.search(foo, idfoo).group()
>>>m
foo

 

python正则表达式match,search,find的使用方法

原文:http://www.cnblogs.com/lmt921108/p/8024593.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!