首页 / 正则 / python正则表达式3-模式匹配
python正则表达式3-模式匹配
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python正则表达式3-模式匹配,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1986字,纯文字阅读大概需要3分钟。
内容图文
![python正则表达式3-模式匹配](/upload/InfoBanner/zyjiaocheng/1087/b3f0fb99c4d8451d83a37b33971ad7f6.jpg)
re.S,使 ‘.‘ 匹配换行在内的所有字符
>>> pattern=r‘ghostwu.com‘ >>> import re >>> re.findall( pattern, ‘ghostwuacom‘ ) [‘ghostwuacom‘] >>> re.findall( pattern, ‘ghostwubcom‘ ) [‘ghostwubcom‘] >>> re.findall( pattern, ‘ghostwu.com‘ ) [‘ghostwu.com‘] >>> re.findall( pattern, ‘ghostwu\ncom‘ ) [] >>> re.findall( pattern, ‘ghostwu\ncom‘, re.S ) [‘ghostwu\ncom‘] >>>
re.M,多行匹配,主要影响( ^和$ )
>>> str=""" ... hi,ghostwu,how are you ... ghostwu: my name is ghostwu,how are you ... ghostwu: nice to meet you ... hello ghostwu ... """ >>> pattern = r"^ghostwu" >>> re.findall( pattern, str ) [] >>> re.findall( pattern, str, re.M ) [‘ghostwu‘, ‘ghostwu‘] >>>
当正则有多行的时候,可以开启verbose模式re.X
>>> pattern=r""" ... \d{3,4} ... -? ... \d{8} ... """ >>> str="020-88888888" >>> re.findall( pattern, str ) [] >>> re.findall( pattern, str, re.X ) [‘020-88888888‘] >>>
():分组与| 的使用, 假如我们要匹配一个.com,.cn,.net结尾的email
>>> pattern=r"\w+@\w+(.com|.cn|.net)" >>> email="abc@qq.com">>> re.match( pattern, email ) <_sre.SRE_Match object at 0x7f2b74481828> >>> re.match( pattern, ‘abc@qq.cn‘ ) <_sre.SRE_Match object at 0x7f2b744818a0> >>> re.match( pattern, ‘abc@qq.net‘ ) <_sre.SRE_Match object at 0x7f2b74481828> >>> re.match( pattern, ‘abc@qq.io‘ ) >>>
匹配超链接
>>> html=""" ... <a href="http://www.baidu.com">百度</a> ... <a href="index.html">首页</a> ... <p>这是一段说明</p> ... <a href="http://www.taobao.com">淘宝</a> ... """ >>> re=r"href=\"(.+?)\"" >>> pattern=r"href=\"(.+?)\"" >>> re ‘href=\\"(.+?)\\"‘ >>> import re >>> re.findall( pattern, html ) [‘http://www.baidu.com‘, ‘index.html‘, ‘http://www.taobao.com‘] >>>
原文:https://www.cnblogs.com/ghostwu/p/8634449.html
内容总结
以上是互联网集市为您收集整理的python正则表达式3-模式匹配全部内容,希望文章能够帮你解决python正则表达式3-模式匹配所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。