设为首页 收藏本站
查看: 1043|回复: 0

[经验分享] Python_正则表达式

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2016-9-5 15:00:26 | 显示全部楼层 |阅读模式
一、使用match()方法匹配字符串
match方法从字符串的起始部分进行匹配,匹配成功,返回,匹配失败返回None
ep:
1
2
3
4
5
6
>>> m = re.match('foo','food')    ##success
>>>
>>>
>>> if m is not None: m.group()
...
'foo'



匹配成功返回foo
1
2
3
4
5
>>> m = re.match('foo','seafood')   ##False
>>>
>>> if m is not None: m.group()
...
>>>



匹配失败返回None
二、使用search()方法匹配字符串
search()方法会从字符串的任意位置进行匹配,匹配成功返回,匹配失败返回None
1
2
3
4
5
>>> m = re.search('foo','seafood')   ##success
>>>
>>> if m is not None: m.group()
...
'foo'



三、匹配多个字符串
1
2
3
4
5
6
>>> bt = "bat|bet|bit"
>>> m = re.match(bt,"bat")
>>> if m is not None: m.group()
...
'bat'
>>>



匹配成功
1
2
3
4
>>> m = re.match(bt,"He bit me")
>>> if m is not None: m.group()
...
>>>



匹配失败
1
2
3
4
>>> m = re.search(bt,"He bit me")
>>> if m is not None: m.group()
...
'bit'



匹配成功
四、匹配任意单个字符
1
2
3
4
5
>>> anyend = ".end"
>>> m = re.match(anyend,"bend")
>>> if m is not None: m.group()
...
'bend'



匹配成功
1
2
3
4
5
>>> m = re.search(".end","Tome end")
>>>
>>> if m is not None: m.group()
...
' end'



匹配成功
如果是真正的小数点,使用\.
五、字符集 ([ ])
1
2
3
4
>>> m = re.match('[cr][23][dp][o2]','c2po')
>>> if m is not None: m.group()
...
'c2po'



成功匹配c2po
1
2
3
4
5
>>> m = re.match('r2d2|c3po','r2d2')
>>>
>>> if m is not None: m.group()
...
'r2d2'



成功匹配r2d2
六、重复、特殊字符以及分组
1
2
3
4
5
6
>>> patt = "\w+@(\w+\.)*\w+\.com"
>>> m = re.match(patt,'abc@staff.sina.cn.com')
>>>
>>> if m is not None: m.group()
...
'abc@staff.sina.cn.com'



上面的正则表达式完成对邮箱的匹配。
匹配字母+数字
1
2
3
4
>>> m = re.match('\w\w\w-\d\d\d','abc-123')
>>> if m is not None: m.group()
...
'abc-123'



或者
1
2
3
4
>>> m = re.match('(\w)+-(\d)+','abc-123')
>>> if m is not None: m.group()
...
'abc-123'



子组:使用圆括号来匹配和保存数组
groups()方法:匹配包含所有子组的元组。
1
2
3
4
5
6
7
8
9
10
>>> m = re.match('(\w\w\w)-(\d\d\d)','abc-123')
>>> if m is not None: m.group(1)               #子组1
...
'abc'
>>> if m is not None: m.group(2) #子组2
...
'123'
>>> if m is not None: m.groups() #全部子组
...
('abc', '123')



由上面可知:
group(1)代表子组1,group(2)代表子组2
groups()获取一个包含所有匹配子组的元组
七、匹配字符串的起始和结尾以及单词边界
1
2
3
>>> m = re.search(r'\bthe',"this the dog ")  #r作用指定是正则表达式
>>> m.group()
'the'



匹配the单词
1
2
3
>>> m = re.search(r'\Bthe',"bitthes the dog ")
>>> m.group()
'the'



\B与之相反,匹配单词中的the
八、使用findall()以及finditer()
findall():以列表的形式返回字符串
1
2
>>> re.findall('a','this is a car and a shu')
['a', 'a', 'a', 'a']



出现了四次a,打印四个a的列表
1
2
3
4
5
6
7
8
9
10
11
>>> s
'This and that'
>>> re.findall(r'(th\w+) and (th\w+)', s, re.I )  #re.I忽略大小写
[('This', 'that')]
finditer:
>>> re.finditer(r'(th\w+) and (th\w+)', s, re.I ).next().groups()
('This', 'that')
>>> re.finditer(r'(th\w+) and (th\w+)', s, re.I ).next().group(1)
'This'
>>> re.finditer(r'(th\w+) and (th\w+)', s, re.I ).next().group(2)
'that'



finditer():使用了迭代的方式进行检索,并把结果输出出来。
九、使用sub及subn进行搜索与替换
1
2
>>> re.sub('a','A',"This is a apple")
'This is A Apple'



将a替换成A
1
2
>>> re.subn('a','A',"This is a apple")
('This is A Apple', 2)



使用subs替换过之后,还将替换的个数打印出来。
匹配对象的group除了能够取出匹配分组的编号之外,还可以使用\N,其中N是分组中的编号:
1
2
>>> re.sub(r'(\d{1,2})/(\d{1,2})/(\d{2}|\d{4})',r'\2/\1/\3','2/20/91' )
'20/2/91'



例子中\2 \1 \3分别代表三个分组
十、split分割字符串
1
2
>>> re.split(':','a:b:c')
['a', 'b', 'c']



十一、compile模块
compile创建模式对象
1
2
3
4
>>> pat=re.compile('A')
>>> m=pat.search('CBA')
>>> m.group()
'A'



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-268010-1-1.html 上篇帖子: python分析nginx日志的ip(来源) 下篇帖子: Python操作redis的订阅发布功能 交换机 表达式
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表