2023年12月14日发(作者:)

【python】re模块的用法(1)——基本语法

Python 的 re 模块(Regular Expression 正则表达式)提供各种正则表达式的匹配操作,在文本解析、复杂字符串分析和信息提取时是

一个非常有用的工具,下面我主要总结了re的常用方法。

的简介

使用python的re模块,尽管不能满足所有复杂的匹配情况,但足够在绝大多数情况下能够有效地实现对复杂字符串的分析并提取出相关

信息。python 会将正则表达式转化为字节码,利用 C 语言的匹配引擎进行深度优先的匹配。

1. import re

2. print re.__doc__

可以查询re模块的功能信息,下面会结合几个例子说明。

的正则表达式语法

正则表达式语法表如下:

语法

"."

"^"

"$"

"*"

"+"

"?"

*?,+?,??

{m,n}

{m,n}?

""

[]

"|"

(...)

(?#...)

(?=...)

(?!...)

(?<=...)

(?

意义

任意字符

字符串开始

字符串结尾

0 个或多个字符(贪婪匹配)

1 个或多个字符(贪婪匹配)

0 个或多个字符(贪婪匹配)

以上三个取第一个匹配结果(非贪婪匹配)

对于前一个字符重复m到n次,{m}亦可

对于前一个字符重复m到n次,并取尽可能少

特殊字符转义或者特殊序列

表示一个字符集

匹配括号中任意表达式

注释,可忽略

Matches if ... matches next, but doesn't consume the string.

Matches if ... doesn't match next.

Matches if preceded by ... (must be fixed length).

Matches if not preceded by ... (must be fixed length).

说明

'^hello'匹配'helloworld'而不匹配'aaaahellobbb'

与上同理

<*>匹配chinaunix

与上同理

与上同理

<*>匹配</p><p>a{6}匹配6个a、a{2,4}匹配2到4个a</p><p>‘aaaaaa’中a{2,4}只会匹配2个</p><p> </p><p>[0-9]、[a-z]、[A-Z]、[^0]</p><p>A|B,或运算</p><p> </p><p> </p><p>'(?=test)' 在hellotest中匹配hello</p><p>'(?!=test)' 若hello后面不为test,匹配hello</p><p>'(?<=hello)test' 在hellotest中匹配test</p><p>'(?<!hello)test' 在hellotest中不匹配test</p><p> 正则表达式特殊序列表如下:</p><p>特殊序列符号</p><p>A</p><p>Z</p><p>b</p><p>B</p><p>意义</p><p>只在字符串开始进行匹配</p><p>只在字符串结尾进行匹配</p><p>匹配位于开始或结尾的空字符串</p><p>匹配不位于开始或结尾的空字符串</p><p>d</p><p>D</p><p>s</p><p>S</p><p>w</p><p>W</p><p>相当于[0-9]</p><p>相当于[^0-9]</p><p>匹配任意空白字符:[tnrrv]</p><p>匹配任意非空白字符:[^tnrrv]</p><p>匹配任意数字和字母:[a-zA-Z0-9]</p><p>匹配任意非数字和字母:[^a-zA-Z0-9]</p><p>match</p><p>(pattern, string[, flags])</p><p>match(string[, pos[, endpos]])</p><p>作用:match() 函数只在字符串的开始位置尝试匹配正则表达式,也就是只报告从位置 0 开始的匹配情况,而 search() 函数是扫描整个字</p><p>符串来查找匹配。如果想要搜索整个字符串来寻找匹配,应当用 search()。</p><p>下面是几个例子:</p><p>例:最基本的用法,通过bject对象调用</p><p>1. #!/usr/bin/env python</p><p>2. import re</p><p>3. </p><p>4. r1 = e(r'world')</p><p>5. if ('helloworld'):</p><p>6. print 'match succeeds'</p><p>7. else:</p><p>8. print 'match fails'</p><p>9. </p><p>10. if ('helloworld'):</p><p>11. print 'search succeeds'</p><p>12. else:</p><p>13. print 'search fails'</p><p>说明一下:r是raw(原始)的意思。因为在表示字符串中有一些转义符,如表示回车'n'。如果要表示表需要写为''。但如果我就是需要表</p><p>示一个''+'n',不用r方式要写为:'n'。但使用r方式则为r'n'这样清晰多了。</p><p>例:设置flag</p><p>1. #r2 = e(r'n$', re.S)</p><p>2. #r2 = e('n$', re.S)</p><p>3. r2 = e('World$', re.I)</p><p>4. if ('helloworldn'):</p><p>5. print 'search succeeds'</p><p>6. else:</p><p>7. print 'search fails'</p><p>8. </p><p>例:直接调用</p><p>1. if (r'abc','helloaaabcdworldn'):</p><p>2. print 'search succeeds'3. else:</p><p>4. print 'search fails'</p><p>split</p><p>(pattern, string[, maxsplit=0, flags=0])</p><p>split(string[, maxsplit=0])</p><p>作用:可以将字符串匹配正则表达式的部分割开并返回一个列表</p><p>例:简单分析ip</p><p>1. #!/usr/bin/env python</p><p>2. import re</p><p>3. r1 = e('W+')</p><p>4. print ('192.168.1.1')</p><p>5. print ('(W+)','192.168.1.1')</p><p>6. print ('(W+)','192.168.1.1',</p><p>1)</p><p>结果如下:</p><p>['192', '168', '1', '1']</p><p>['192', '.', '168', '.', '1', '.', '1']</p><p>['192', '.', '168.1.1']</p><p>findall</p><p>l(pattern, string[, flags])</p><p>findall(string[, pos[, endpos]])</p><p>作用:在字符串中找到正则表达式所匹配的所有子串,并组成一个列表返回</p><p>例:查找[]包括的内容(贪婪和非贪婪查找)</p><p>1. #!/usr/bin/env python</p><p>2. import re</p><p>3. </p><p>4. r1 = e('([.*])')</p><p>5. print l(r1,"hello[hi]heldfsdsf[iwonder]lo")</p><p>6. r1 = e('([.*?])')</p><p>7. print l(r1,"hello[hi]heldfsdsf[iwonder]lo")</p><p>8. </p><p>9. print l('[0-9]{2}',"fdskfj1323jfkdj")</p><p>10. print l('([0-9][a-z])',"fdskfj1323jfkdj")</p><p>11. print l('(?=www)',"afdsfwwwfkdjfsdfsdwww")</p><p>12. print l('(?<=www)',"afdsfwwwfkdjfsdfsdwww")</p><p>finditer</p><p>er(pattern, string[, flags])</p><p>finditer(string[, pos[, endpos]])</p><p>说明:和 findall 类似,在字符串中找到正则表达式所匹配的所有子串,并组成一个迭代器返回。同样 RegexObject 有:</p><p>sub</p><p>(pattern, repl, string[, count, flags])</p><p>sub(repl, string[, count=0])</p><p>说明:在字符串 string 中找到匹配正则表达式 pattern 的所有子串,用另一个字符串 repl 进行替换。如果没有找到匹配 pattern 的串,</p><p>则返回未被修改的 string。Repl 既可以是字符串也可以是一个函数。</p><p>例:1. #!/usr/bin/env python2. import re3. </p><p>4. p = e('(one|two|three)')5. print ('num','one word two words three wordsapple', 2)(pattern, repl, string[, count, flags])subn(repl, string[, count=0])说明:该函数的功能和 sub() 相同,但它还返回新的字符串以及替换的次数。同样 RegexObject 有: </p><p> </p><p><p></p></img></p></div></article></div><div class="fzithome-com info"><div><span>本文发布于:2023-12-14,感谢您对本站的认可!</span></div><div><span>本文链接:</span><a href="https://www.fzithome.com/xitong/1702503717a65544.html" title="【python】re模块的用法(1)——基本语法">https://www.fzithome.com/xitong/1702503717a65544.html</a></div><div><span>版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。</span></div></div><span class="tag"><i class="iconfont icon-discount" style="font-size:12px;font-weight:bold;opacity:.7;">本文标签:</i><a href="/tag/1711.html" target="_blank">匹配</a><a href="/tag/1731.html" target="_blank">字符串</a><a href="/tag/966.html" target="_blank">模块</a><a href="/tag/1264.html" target="_blank">开始</a><a href="/tag/243.html" target="_blank">表示</a></span></div><div class="fzithome-com post_comments" id="comments"><div id="comt-respond" class="commentpost"><h4>发布评论 <span><a rel="nofollow" id="cancel-reply" href="#comment" style="display:none;"><small>取消回复</small></a></span></h4><form action="/comment/create/65544.html?safe_token=d1QE7ZO1z2b6Q5_2FB8XrgkXgRJytBCTWfkafOYbJyqMvvZ26XF1lJoxV2dJouf_2B5jlB3M_2FUWvHBfK1PGreRIpqw_3D_3D" method="post" name="saypl" id="frmSumbit"><input type="hidden" name="doctype" value="1" /><input type="hidden" name="quotepid" value="0" /><div id="comment-tools"><div class="fzithome-com tools_text"><textarea placeholder="请在这里留言..." name="message" id="txaArticle" class="text input-block-level comt-area" cols="50" rows="4" tabindex="5"></textarea></div></div><div class="fzithome-com psumbit"><input name="sumbit" type="submit" tabindex="6" value="发布" class="button" /></div></form></div><div class="fzithome-com commentlist"><div class="fzithome-com comment-tab"><div class="fzithome-com come-comt">评论列表<span id="comment_count">(有<span id="infocommentnumarea" style="color:#c81111">0</span>条评论)</span></div></div><ul class="diy-comment"></ul></div></div></div><div class="fzithome-com mainr"><div class="widget widget_previous"><h4 class="bar">最近发表</h4><ul><li><a href="/xitong/1700738039a58.html"title='同望操作技巧_secret' aria-label='同望操作技巧_secret'>同望操作技巧_secret</a></li><li><a href="/xitong/1700738155a64.html"title='WordExcel与Access之间的数据交换' aria-label='WordExcel与Access之间的数据交换'>WordExcel与Access之间的数据交换</a></li><li><a href="/xitong/1700739811a136.html"title='CXAX快捷键' aria-label='CXAX快捷键'>CXAX快捷键</a></li><li><a href="/xitong/1700739998a139.html"title='神机软件中YDB格式的导入导出' aria-label='神机软件中YDB格式的导入导出'>神机软件中YDB格式的导入导出</a></li><li><a href="/xitong/1700741093a194.html"title='GIS与几种软件之间的数据转换方法' aria-label='GIS与几种软件之间的数据转换方法'>GIS与几种软件之间的数据转换方法</a></li><li><a href="/xitong/1700742551a257.html"title='(完整)labview与数据库资料' aria-label='(完整)labview与数据库资料'>(完整)labview与数据库资料</a></li><li><a href="/xitong/1700742708a263.html"title='sql中如何调用另一台服务器的数据库查询数据呢?' aria-label='sql中如何调用另一台服务器的数据库查询数据呢?'>sql中如何调用另一台服务器的数据库查询数据呢?</a></li><li><a href="/xitong/1700746364a443.html"title='如何彻底清除注册表垃圾' aria-label='如何彻底清除注册表垃圾'>如何彻底清除注册表垃圾</a></li><li><a href="/xitong/1700748193a533.html"title='百度知道,本月高分热点问题精选' aria-label='百度知道,本月高分热点问题精选'>百度知道,本月高分热点问题精选</a></li><li><a href="/xitong/1700748484a546.html"title='看生产企业如何应对' aria-label='看生产企业如何应对'>看生产企业如何应对</a></li><li><a href="/xitong/1700752766a755.html"title='新媒体选题如何紧扣热点?∣选题必备工具箱' aria-label='新媒体选题如何紧扣热点?∣选题必备工具箱'>新媒体选题如何紧扣热点?∣选题必备工具箱</a></li><li><a href="/xitong/1700753598a797.html"title='新闻传播中的网络舆情热点分析' aria-label='新闻传播中的网络舆情热点分析'>新闻传播中的网络舆情热点分析</a></li><li><a href="/xitong/1700759861a1097.html"title='联想笔记本电脑一键还原怎么用' aria-label='联想笔记本电脑一键还原怎么用'>联想笔记本电脑一键还原怎么用</a></li><li><a href="/xitong/1700762645a1249.html"title='电脑重装系统后怎么恢复数据' aria-label='电脑重装系统后怎么恢复数据'>电脑重装系统后怎么恢复数据</a></li><li><a href="/xitong/1700763160a1275.html"title='浅谈农村中小学计算机教室管理' aria-label='浅谈农村中小学计算机教室管理'>浅谈农村中小学计算机教室管理</a></li><li><a href="/xitong/1700764717a1365.html"title='电脑运行时发热严重学会优化让电脑温控完美' aria-label='电脑运行时发热严重学会优化让电脑温控完美'>电脑运行时发热严重学会优化让电脑温控完美</a></li><li><a href="/xitong/1700765928a1433.html"title='台式机显卡温度过高怎么办' aria-label='台式机显卡温度过高怎么办'>台式机显卡温度过高怎么办</a></li><li><a href="/xitong/1700765960a1435.html"title='电脑风扇清洁及保养方法' aria-label='电脑风扇清洁及保养方法'>电脑风扇清洁及保养方法</a></li><li><a href="/xitong/1700767379a1515.html"title='如何使您的电脑变得快一点' aria-label='如何使您的电脑变得快一点'>如何使您的电脑变得快一点</a></li><li><a href="/xitong/1700767546a1524.html"title='电脑定格死机怎么回事' aria-label='电脑定格死机怎么回事'>电脑定格死机怎么回事</a></li></ul></div><section id="aside_about" class="widget widget_aside_about sb br mb"><div class="avatar"><img class="img" src="/view/template/mitiqin/img/tx.jpg" alt="福州电脑网_福州电脑维修_福州电脑之家_福州iThome"/></div><div class="wrap pd"><p class="title">福州电脑网_福州电脑维修_福州电脑之家_福州iThome</p><p class="info">福州电脑维修网(fzithome.com)专业的电脑维修,笔记本维修,上门维修各种电脑,笔记本,平板等,快速上门.电脑知识频道内容覆盖:计算机资讯,电脑基础应用知识,各种电脑故障维修学习,电脑外设产品维修维护,病毒,软件,硬件,常识.</p><ul class="ul clearfix"></ul></div></section><div class="fzithome-com clear"></div><div class="fzithome-com widgets"><h4 class="bar">相关推荐</h4><div class="fzithome-com hot-post"><ul class="clearfix"><li><a href="/biancheng/1771306473a2684764.html"title='初学者必看!宏基笔记本触摸板开启关闭超详细步骤' aria-label='初学者必看!宏基笔记本触摸板开启关闭超详细步骤'><span class="sptit">初学者必看!宏基笔记本触摸板开启关闭超详细步骤</span></a></li><li><a href="/biancheng/1771460208a2686518.html"title='告别电脑卡顿:彻底排查并修复影响Windows关机效率的问题' aria-label='告别电脑卡顿:彻底排查并修复影响Windows关机效率的问题'><span class="sptit">告别电脑卡顿:彻底排查并修复影响Windows关机效率的问题</span></a></li><li><a href="/xitong/1771637991a2688516.html"title='系统重置后,你还在等什么?为何360不给更新Flash Player?' aria-label='系统重置后,你还在等什么?为何360不给更新Flash Player?'><span class="sptit">系统重置后,你还在等什么?为何360不给更新Flash Player?</span></a></li><li><a href="/biancheng/1771783748a2690159.html"title='Linux中的换行符:与Dos有何不同?深度解读' aria-label='Linux中的换行符:与Dos有何不同?深度解读'><span class="sptit">Linux中的换行符:与Dos有何不同?深度解读</span></a></li><li><a href="/xitong/1771817828a2690551.html"title='清除恶意软件影响,让Flash中心重新闪亮' aria-label='清除恶意软件影响,让Flash中心重新闪亮'><span class="sptit">清除恶意软件影响,让Flash中心重新闪亮</span></a></li><li><a href="/xitong/1772427662a2696463.html"title='一键修复 - 双击无法打开本地磁盘的问题' aria-label='一键修复 - 双击无法打开本地磁盘的问题'><span class="sptit">一键修复 - 双击无法打开本地磁盘的问题</span></a></li><li><a href="/xitong/1772594218a2697701.html"title='2022 ICPC亚洲大赛首站,沈阳站激烈角逐' aria-label='2022 ICPC亚洲大赛首站,沈阳站激烈角逐'><span class="sptit">2022 ICPC亚洲大赛首站,沈阳站激烈角逐</span></a></li><li><a href="/xitong/1772605594a2697828.html"title='六足机器人代码大全:实现机器人行走新体验' aria-label='六足机器人代码大全:实现机器人行走新体验'><span class="sptit">六足机器人代码大全:实现机器人行走新体验</span></a></li><li><a href="/biancheng/1772657578a2698391.html"title='如何在NTFS与FAT32之间切换?简单易懂的操作指南!' aria-label='如何在NTFS与FAT32之间切换?简单易懂的操作指南!'><span class="sptit">如何在NTFS与FAT32之间切换?简单易懂的操作指南!</span></a></li><li><a href="/biancheng/1773211798a2699062.html"title='从零开始:解读TSUNG XML配置文件,打造高效测试环境' aria-label='从零开始:解读TSUNG XML配置文件,打造高效测试环境'><span class="sptit">从零开始:解读TSUNG XML配置文件,打造高效测试环境</span></a></li><li><a href="/xitong/1773225954a2699206.html"title='远离自检干扰:高效技巧让你的电脑启动更快更顺畅' aria-label='远离自检干扰:高效技巧让你的电脑启动更快更顺畅'><span class="sptit">远离自检干扰:高效技巧让你的电脑启动更快更顺畅</span></a></li><li><a href="/xitong/1773334749a2700148.html"title='告别慢吞吞,3步搞定电脑性能提速' aria-label='告别慢吞吞,3步搞定电脑性能提速'><span class="sptit">告别慢吞吞,3步搞定电脑性能提速</span></a></li><li><a href="/biancheng/1773439631a2701308.html"title='IE8卸载秘籍:让你快速释放Flash播放器的新功能' aria-label='IE8卸载秘籍:让你快速释放Flash播放器的新功能'><span class="sptit">IE8卸载秘籍:让你快速释放Flash播放器的新功能</span></a></li><li><a href="/biancheng/1773476843a2701715.html"title='轻松破解Windows中无法删除的Flash文件夹,一招搞定!' aria-label='轻松破解Windows中无法删除的Flash文件夹,一招搞定!'><span class="sptit">轻松破解Windows中无法删除的Flash文件夹,一招搞定!</span></a></li><li><a href="/biancheng/1773635712a2703512.html"title='电脑玩不动游戏?快速修复SWF、Flash中心卡顿的妙招' aria-label='电脑玩不动游戏?快速修复SWF、Flash中心卡顿的妙招'><span class="sptit">电脑玩不动游戏?快速修复SWF、Flash中心卡顿的妙招</span></a></li><li><a href="/xitong/1773714837a2704184.html"title='从CDA文件包头开始:解读音频CDA中的CD-Text指南' aria-label='从CDA文件包头开始:解读音频CDA中的CD-Text指南'><span class="sptit">从CDA文件包头开始:解读音频CDA中的CD-Text指南</span></a></li><li><a href="/xitong/1773954275a2706828.html"title='CPU 使用率和负载Load_cpu load' aria-label='CPU 使用率和负载Load_cpu load'><span class="sptit">CPU 使用率和负载Load_cpu load</span></a></li><li><a href="/xitong/1773954428a2706830.html"title='【CPU】如何正确理解 CPU 使用率和平均负载的关系?_cpu使用率和负载的关系' aria-label='【CPU】如何正确理解 CPU 使用率和平均负载的关系?_cpu使用率和负载的关系'><span class="sptit">【CPU】如何正确理解 CPU 使用率和平均负载的关系?_cpu使用率和负载的关系</span></a></li><li><a href="/xitong/1774283835a2709083.html"title='明白了Chkdsk工具的使用方法' aria-label='明白了Chkdsk工具的使用方法'><span class="sptit">明白了Chkdsk工具的使用方法</span></a></li><li><a href="/xitong/1774373326a2710089.html"title='utorun.inf病毒查杀:教你清除autorun.inf病毒_autorun.inf 专杀' aria-label='utorun.inf病毒查杀:教你清除autorun.inf病毒_autorun.inf 专杀'><span class="sptit">utorun.inf病毒查杀:教你清除autorun.inf病毒_autorun.inf 专杀</span></a></li></ul></div></div><div class="fzithome-com clear"></div><div class="widget widget_tags"><h4 class="bar">标签列表</h4><ul><li class="submenu"><a target="_blank" href="/tag/244975.html">防止真实</a></li><li class="submenu"><a target="_blank" href="/tag/244942.html">点点好友</a></li><li class="submenu"><a target="_blank" href="/tag/244854.html">角的</a></li><li class="submenu"><a target="_blank" href="/tag/244843.html">树状视图</a></li><li class="submenu"><a target="_blank" href="/tag/244831.html">深潜者之</a></li><li class="submenu"><a target="_blank" href="/tag/244789.html">微信客户</a></li><li class="submenu"><a target="_blank" href="/tag/244767.html">安装搜狗</a></li><li class="submenu"><a target="_blank" href="/tag/244763.html">模拟耳机</a></li><li class="submenu"><a target="_blank" href="/tag/244756.html">此网站的</a></li><li class="submenu"><a target="_blank" href="/tag/244749.html">的性能</a></li><li class="submenu"><a target="_blank" href="/tag/244745.html">多线程与</a></li><li class="submenu"><a target="_blank" href="/tag/244634.html">简单合并</a></li><li class="submenu"><a target="_blank" href="/tag/244630.html">频怎么无</a></li><li class="submenu"><a target="_blank" href="/tag/244629.html">成其它格</a></li><li class="submenu"><a target="_blank" href="/tag/244598.html">一个设备</a></li><li class="submenu"><a target="_blank" href="/tag/244593.html">抖音小程</a></li><li class="submenu"><a target="_blank" href="/tag/244565.html">前台窗口</a></li><li class="submenu"><a target="_blank" href="/tag/244561.html">在一些情</a></li><li class="submenu"><a target="_blank" href="/tag/244541.html">中文界面</a></li><li class="submenu"><a target="_blank" href="/tag/244500.html">需要在</a></li></ul></div><div class="fzithome-com clear"></div></div></div><footer id="footer"><div class="footer container-w cl"><div class="fnav"></div><span class="copy"> CopyRight © 2022 All Rights Reserved <a href="/" title="福州电脑网_福州电脑维修_福州电脑之家_福州iThome" target="_blank">福州电脑网_福州电脑维修_福州电脑之家_福州iThome</a></span> 备案号:<a target="_blank" rel="nofollow" href="https://beian.miit.gov.cn/" style="font-size: 12px;">豫ICP备2022026798号-13</a></div></footer><script src="/view/template/mitiqin/js/common.min.js?2.3.0"></script><script src="/view/js/xiuno.js"></script><script src="/view/template/mitiqin/js/app.js?2.3.0"></script><div id="gotop"><div style="display: none; margin-bottom:5px;" id="goTopBtn"><a title="返回顶部" class="gotopa"><span class="iconfont icon-rocket-fill"></span></a></div></div></body></html>