砚水凝 發表於 2019-10-10 18:16:00

Python之re模块

<h2>一、正则表达式  &nbsp;</h2>
<p>  re模块是python独有的匹配字符串的模块,该模块中提供的很多功能是基于正则表达式实现的,而正则表达式是对字符串进行模糊匹配,提取自己需要的字符串部分,他对所有的语言都通用。注意:</p>
<ul>
<li>re模块是python独有的</li>
<li>正则表达式所有编程语言都可以使用</li>
<li>re模块、正则表达式是对字符串进行操作</li>
</ul>
<p>因为,re模块中的方法大都借助于正则表达式,故先学习正则表达式。</p>
<h3>(一)常用正则</h3>
<h4>&nbsp;1、字符组</h4>
<p>在同一个位置可能出现的各种字符组成了一个字符组,在正则表达式中用[]表示</p>
<table style="width: 80%" border="0" align="center">
<tbody>
<tr>
<td style="text-align: center" width="105px">
<pre>正则</pre>
</td>
<td style="text-align: center" width="15px">
<pre>待匹配字符</pre>
</td>
<td style="text-align: center" width="45px">
<pre>匹配<br>结果</pre>
</td>
<td style="text-align: center" width="250px">
<pre>说明</pre>
</td>
</tr>
<tr>
<td style="text-align: center">
<pre></pre>
</td>
<td style="text-align: center">
<pre>8</pre>
</td>
<td style="text-align: center">
<pre>True</pre>
</td>
<td style="text-align: center">
<pre>在一个字符组里枚举合法的所有字符,字符组里的任意一个字符<br>和"待匹配字符"相同都视为可以匹配</pre>
</td>
</tr>
<tr>
<td style="text-align: center">
<pre></pre>
</td>
<td style="text-align: center">
<pre>a</pre>
</td>
<td style="text-align: center">
<pre>False</pre>
</td>
<td style="text-align: center">
<pre>由于字符组中没有"a"字符,所以不能匹配</pre>
</td>
</tr>
<tr>
<td style="text-align: center">&nbsp;
<pre></pre>
</td>
<td style="text-align: center">&nbsp;
<pre>7</pre>
</td>
<td style="text-align: center">
<pre>True</pre>
</td>
<td style="text-align: center">
<pre>也可以用-表示范围,就和是一个意思</pre>
</td>
</tr>
<tr>
<td style="text-align: center">&nbsp;
<pre></pre>
</td>
<td style="text-align: center">&nbsp;
<pre>s</pre>
</td>
<td style="text-align: center">&nbsp;
<pre>True</pre>
</td>
<td style="text-align: center">&nbsp;
<pre>同样的如果要匹配所有的小写字母,直接用就可以表示</pre>
</td>
</tr>
<tr>
<td style="text-align: center">&nbsp;
<pre></pre>
</td>
<td style="text-align: center">&nbsp;
<pre>B</pre>
</td>
<td style="text-align: center">&nbsp;
<pre>True</pre>
</td>
<td style="text-align: center">&nbsp;
<pre>就表示所有的大写字母</pre>
</td>
</tr>
<tr>
<td style="text-align: center">&nbsp;
<pre></pre>
</td>
<td style="text-align: center">&nbsp;
<pre>e</pre>
</td>
<td style="text-align: center">&nbsp;
<pre>True</pre>
</td>
<td style="text-align: center">&nbsp;
<pre>可以匹配数字,大小写形式的a~f,用来验证十六进制字符</pre>
</td>
</tr>
</tbody>
</table>
<h4>&nbsp;2、字符</h4>
<table style="width: 80%" border="0" align="center">
<tbody>
<tr>
<td style="text-align: center">&nbsp;
<pre>元字符</pre>
</td>
<td style="text-align: center">&nbsp;
<pre>匹配内容</pre>
</td>
</tr>
<tr>
<td style="text-align: center">.&nbsp;</td>
<td style="text-align: center">匹配除换行符以外的任意字符</td>
</tr>
<tr>
<td style="text-align: center">\w</td>
<td style="text-align: center">匹配字母或数字或下划线</td>
</tr>
<tr>
<td style="text-align: center">\s</td>
<td style="text-align: center">匹配任意的空白符</td>
</tr>
<tr>
<td style="text-align: center">\d</td>
<td style="text-align: center">匹配数字</td>
</tr>
<tr>
<td style="text-align: center">\n</td>
<td style="text-align: center">匹配一个换行符</td>
</tr>
<tr>
<td style="text-align: center">\t</td>
<td style="text-align: center">匹配一个制表符</td>
</tr>
<tr>
<td style="text-align: center">\b</td>
<td style="text-align: center">匹配一个单词的结尾</td>
</tr>
<tr>
<td style="text-align: center">^</td>
<td style="text-align: center">匹配字符串的开始</td>
</tr>
<tr>
<td style="text-align: center">$</td>
<td style="text-align: center">匹配字符串的结尾</td>
</tr>
<tr>
<td style="text-align: center">\W</td>
<td style="text-align: center">
<pre>匹配非字母或数字或下划线</pre>
</td>
</tr>
<tr>
<td style="text-align: center">\D</td>
<td style="text-align: center">
<pre>匹配<span style="line-height: 1.5">非数字</span></pre>
</td>
</tr>
<tr>
<td style="text-align: center">\S</td>
<td style="text-align: center">
<pre>匹配<span style="line-height: 1.5">非空白符</span></pre>
</td>
</tr>
<tr>
<td style="text-align: center">a|b</td>
<td style="text-align: center">
<pre>匹配字符a或字符b</pre>
</td>
</tr>
<tr>
<td style="text-align: center">()</td>
<td style="text-align: center">
<pre>匹配括号内的表达式,也表示一个组</pre>
</td>
</tr>
<tr>
<td style="text-align: center">[...]</td>
<td style="text-align: center">
<pre>匹配字符组中的字符</pre>
</td>
</tr>
<tr>
<td style="text-align: center">[^...]</td>
<td style="text-align: center">
<pre>匹配除了字符组中字符的所有字符</pre>
</td>
</tr>
</tbody>
</table>
<h4>3、量词</h4>
<table style="width: 80%" border="0" align="center">
<tbody>
<tr>
<td style="text-align: center">
<pre>量词</pre>
</td>
<td style="text-align: center">
<pre>用法说明</pre>
</td>
</tr>
<tr>
<td style="text-align: center">*</td>
<td style="text-align: center">重复零次或更多次</td>
</tr>
<tr>
<td style="text-align: center">+</td>
<td style="text-align: center">重复一次或更多次</td>
</tr>
<tr>
<td style="text-align: center">?</td>
<td style="text-align: center">重复零次或一次</td>
</tr>
<tr>
<td style="text-align: center">{n}</td>
<td style="text-align: center">重复n次</td>
</tr>
<tr>
<td style="text-align: center">{n,}</td>
<td style="text-align: center">重复n次或更多次</td>
</tr>
<tr>
<td style="text-align: center">{n,m}</td>
<td style="text-align: center">重复n到m次</td>
</tr>
</tbody>
</table>
<h3>(二)正则表达式的使用</h3>
<h4>1、. ^ $</h4>
<table style="width: 80%" border="0" align="center">
<tbody>
<tr>
<td style="text-align: center">正则</td>
<td style="text-align: center">待匹配字符</td>
<td style="text-align: center">匹配结果</td>
<td style="text-align: center">说明</td>
</tr>
<tr>
<td style="text-align: center">a.</td>
<td style="text-align: center">abacad</td>
<td style="text-align: center">abacad</td>
<td style="text-align: center">匹配所有"a."的字符</td>
</tr>
<tr>
<td style="text-align: center">^a.</td>
<td style="text-align: center">abacad</td>
<td style="text-align: center">ab</td>
<td style="text-align: center">只从开头匹配"a."</td>
</tr>
<tr>
<td style="text-align: center">a.$</td>
<td style="text-align: center">abacad</td>
<td style="text-align: center">ad</td>
<td style="text-align: center">只匹配结尾的"a.$"</td>
</tr>
</tbody>
</table>
<h4>&nbsp;2、* + ? { }</h4>
<table style="width: 80%" border="0" align="center">
<tbody>
<tr>
<td style="text-align: center">正则</td>
<td style="text-align: center">待匹配字符</td>
<td style="text-align: center">匹配结果</td>
<td style="text-align: center">说明</td>
</tr>
<tr>
<td style="text-align: center">a.?</td>
<td style="text-align: center">abefacgad</td>
<td style="text-align: center">ab<br>
ac<br>
ad</td>
<td style="text-align: center">
<p align="left">                  ?表示重复零次或一次,即只匹配"a"后面一个任意字符。</p>













</td>













</tr>
<tr>
<td style="text-align: center">a.*</td>
<td style="text-align: center">abefacgad</td>
<td style="text-align: center">abefacgad</td>
<td style="text-align: center">*表示重复零次或多次,即匹配"a"后面0或多个任意字符。</td>













</tr>
<tr>
<td style="text-align: center">a.+</td>
<td style="text-align: center">abefacgad</td>
<td style="text-align: center">abefacgad</td>
<td style="text-align: center">+表示重复一次或多次,即只匹配"a"后面1个或多个任意字符。</td>













</tr>
<tr>
<td style="text-align: center">a.{1,2}</td>
<td style="text-align: center">abefacgad</td>
<td style="text-align: center">abe<br>
acg<br>
ad</td>
<td style="text-align: center">{1,2}匹配1到2次任意字符。</td>













</tr>













</tbody>













</table>
<p>注意:前面的*,+,?等都是贪婪匹配,也就是尽可能匹配,后面加?号使其变成惰性匹配</p>
<table style="width: 80%" border="0" align="center">
<tbody>
<tr>
<td style="text-align: center">正则</td>
<td style="text-align: center">待匹配字符</td>
<td style="text-align: center">匹配结果</td>
<td style="text-align: center">说明</td>












</tr>
<tr>
<td style="text-align: center">a.*?</td>
<td style="text-align: center">abefacgad</td>
<td style="text-align: center">
<p>a<br>
a<br>
a</p>












</td>
<td style="text-align: center">惰性匹配</td>












</tr>












</tbody>












</table>
<h4>&nbsp;3、字符集[][^]</h4>
<table style="width: 80%" border="0" align="center">
<tbody>
<tr>
<td style="text-align: center">正则</td>
<td style="text-align: center">待匹配字符</td>
<td style="text-align: center"> 匹配结果
</td>
<td style="text-align: center">
说明
</td>





























































































































































</tr>
<tr>
<td style="text-align: center">a*</td>
<td style="text-align: center">abefacgad</td>
<td style="text-align: center">
<p>abef<br>acg<br>ad</p>






















































































































































</td>
<td style="text-align: center">&nbsp;
<pre>表示匹配"a"后面的字符任意次</pre>
&nbsp;</td>
</tr>
<tr>
<td style="text-align: center">a[^f]*</td>
<td style="text-align: center">abefacgad</td>
<td style="text-align: center">
<p>abe<br>acgad</p>











































































































































</td>
<td style="text-align: center">
<pre>表示匹配一个不是"f"的字符任意次</pre>
</td>
</tr>
<tr>
<td style="text-align: center">[\d]</td>
<td style="text-align: center">412a3bc</td>
<td style="text-align: center">
<p>4<br>1<br>2<br>3</p>










































































































































</td>
<td style="text-align: center">
<pre>表示匹配任意一个数字,匹配到4个结果</pre>
</td>
</tr>
<tr>
<td style="text-align: center">[\d]+</td>
<td style="text-align: center">412a3bc</td>
<td style="text-align: center">
<p>412<br>3</p>

















































































































































</td>
<td style="text-align: center">
<pre>表示匹配任意个数字,匹配到2个结果</pre>
</td>
</tr>
</tbody>
</table>
<h4>&nbsp;4、分组 ()与 或 |[^]</h4>
<p><span style="color: rgba(0, 0, 0, 1); font-family: &quot;PingFang SC&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; font-size: 14px">  身份证号码是一个长度为15或18个字符的字符串,如果是15位则全部由数字组成,首位不能为0;如果是18位,则前17位全部是数字,末位可能是数字或x,下面我们尝试用正则来表示:</span></p>
<table style="width: 80%" border="0" align="center">
<tbody>
<tr>
<td style="text-align: center">正则</td>
<td style="text-align: center">待匹配字符</td>
<td style="text-align: center">匹配结果</td>
<td style="text-align: center">说明</td>
</tr>
<tr>
<td style="text-align: center">^\d{13,16}$</td>
<td style="text-align: center">110101198001017032</td>
<td style="text-align: center">
<p>110101198001017032</p>
</td>
<td style="text-align: center">&nbsp; &nbsp;表示可以匹配一个正确的身份证号</td>
</tr>
<tr>
<td style="text-align: center">^\d{13,16}$</td>
<td style="text-align: center">1101011980010170</td>
<td style="text-align: center">
<p>1101011980010170</p>
</td>
<td style="text-align: center">
<pre>表示也可以匹配这串数字,但这并不是一个正确的身份证号码,它是一个16位的数字</pre>
</td>
</tr>
<tr>
<td style="text-align: center">^\d{14}(\d{2})?$</td>
<td style="text-align: center">1101011980010170</td>
<td style="text-align: center">
<p>False</p>
</td>
<td style="text-align: center">
<pre>现在不会匹配错误的身份证号了<br>()表示分组,将\d{2}分成一组,就可以整体约束他们出现的次数为0-1次</pre>
</td>
</tr>
<tr>
<td style="text-align: center">^(\d{16}|\d{14})$</td>
<td style="text-align: center">110105199812067023</td>
<td style="text-align: center">
<p>110105199812067023</p>
</td>
<td style="text-align: center">
<pre>表示先匹配\d{16}如果没有匹配上就匹配\d{14}</pre>
</td>
</tr>
</tbody>
</table>
<h4>&nbsp;5、转义符 \</h4>
<p>  在正则表达式中,有很多有特殊意义的是元字符,比如\n和\s等,如果要在正则中匹配正常的"\n"而不是"换行符"就需要对"\"进行转义,变成'\\'。</p>
<p>  在python中,无论是正则表达式,还是待匹配的内容,都是以字符串的形式出现的,在字符串中\也有特殊的含义,本身还需要转义。所以如果匹配一次"\n",字符串中要写成'\\n',那么正则里就要写成"\\\\n",这样就太麻烦了。这个时候我们就用到了r'\n'这个概念,此时的正则是r'\\n'就可以了。</p>
<table style="width: 80%" border="0" align="center">
<tbody>
<tr>
<td style="text-align: center">正则</td>
<td style="text-align: center">待匹配字符</td>
<td style="text-align: center">匹配<br>结果</td>
<td style="text-align: center">说明</td>






























































































































</tr>
<tr>
<td style="text-align: center">\n</td>
<td style="text-align: center">\n</td>
<td style="text-align: center">&nbsp;False</td>
<td style="text-align: center">
<pre>因为在正则表达式中\是有特殊意义的字符,所以要匹配\n本身,用表达式\n无法匹配</pre>
</td>
</tr>
<tr>
<td style="text-align: center">\\n</td>
<td style="text-align: center">\n</td>
<td style="text-align: center">&nbsp;True</td>
<td style="text-align: center">
<pre>转义\之后变成\\,即可匹配</pre>
</td>
</tr>
<tr>
<td style="text-align: center">"\\\\n"</td>
<td style="text-align: center">'\\n'</td>
<td style="text-align: center">&nbsp;True</td>
<td style="text-align: center">
<pre>如果在python中,字符串中的'\'也需要转义,所以每一个字符串'\'又需要转义一次</pre>
</td>
</tr>
<tr>
<td style="text-align: center">r'\\n'</td>
<td style="text-align: center">r'\n'</td>
<td style="text-align: center">&nbsp;True</td>
<td style="text-align: center">
<pre>在字符串之前加r,让整个字符串不转义</pre>
</td>
</tr>
</tbody>
</table>
<h4>6、贪婪匹配</h4>
<p>贪婪匹配:在满足匹配时,匹配尽可能长的字符串,默认情况下,采用贪婪匹配</p>
<table style="width: 80%" border="0" align="center">
<tbody>
<tr>
<td style="text-align: center">正则</td>
<td style="text-align: center">待匹配字符</td>
<td style="text-align: center">匹配结果</td>
<td style="text-align: center">说明</td>
</tr>
<tr>
<td style="text-align: center">&lt;.*&gt;</td>
<td style="text-align: center">
<p>&lt;script&gt;...</p>
<p>&lt;script&gt;</p>
</td>
<td style="text-align: center">
<p>&lt;script&gt;...</p>
<p>&lt;script&gt;</p>
</td>
<td style="text-align: center">
<pre>默认为贪婪匹配模式,会匹配尽量长的字符串</pre>
</td>
</tr>
<tr>
<td style="text-align: center">&lt;.*?&gt;</td>
<td style="text-align: center">
<p>&lt;script&gt;...</p>
<p>&lt;script&gt;</p>
</td>
<td style="text-align: center">&nbsp;
<p>&lt;script&gt;<br>&lt;script&gt;</p>

























































































































</td>
<td style="text-align: center">
<pre>加上?为将贪婪匹配模式转为非贪婪匹配模式,会匹配尽量短的字符串</pre>
</td>
</tr>
</tbody>
</table>
<p>几个常用的非贪婪匹配Pattern</p>
<div class="cnblogs_code">
<pre>*<span style="color: rgba(0, 0, 0, 1)">? 重复任意次,但尽可能少重复
</span>+<span style="color: rgba(0, 0, 0, 1)">? 重复1次或更多次,但尽可能少重复
?? 重复0次或1次,但尽可能少重复
{n,m}? 重复n到m次,但尽可能少重复
{n,}? 重复n次以上,但尽可能少重复</span></pre>
</div>
<p>.*?的用法</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">. 是任意字符
</span>*<span style="color: rgba(0, 0, 0, 1)"> 是取 0 至 无限长度
? 是非贪婪模式。
何在一起就是 取尽量少的任意字符,一般不会这么单独写,他大多用在:
.</span>*<span style="color: rgba(0, 0, 0, 1)">?x

就是取前面任意长度的字符,直到一个x出现</span></pre>
</div>
<h2>二、re模块</h2>
<h3>(一)常量、属性</h3>
<h4>1、re.A(re.ASCII)</h4>
<p>让\w,\W,\b,\B,\d,\D,\s和\S 执行ASCII-只匹配完整的Unicode匹配代替。这仅对Unicode模式有意义,而对于字节模式则忽略。</p>
<h4>2、re.I(re.IGNORECASE)</h4>
<p>执行不区分大小写的匹配;类似的表达式也将匹配小写字母。</p>
<h4>3、re.L(re.LOCALE)</h4>
<p>  让\w,\W,\b,\B和区分大小写的匹配取决于当前的语言环境。该标志只能与字节模式一起使用。不建议使用此标志,因为语言环境机制非常不可靠,它一次只能处理一种“区域性”,并且仅适用于8位语言环境。默认情况下,Python 3中已为Unicode(str)模式启用了Unicode匹配,并且能够处理不同的语言环境/语言。</p>
<h4>4、re.M(re.MULTILINE)</h4>
<p>  指定时,模式字符'^'在字符串的开头和每行的开头(紧随每个换行符之后)匹配;模式字符'$'在字符串的末尾和每行的末尾(紧接在每个换行符之前)匹配。默认情况下,'^' 仅在字符串的开头,字符串'$'的末尾和字符串末尾的换行符(如果有)之前立即匹配。</p>
<h4>5、re.S(re.DOTALL)</h4>
<p>使'.'特殊字符与任何字符都匹配,包括换行符;没有此标志,'.'将匹配除换行符以外的任何内容。</p>
<h3>(二)常用方法</h3>
<h4>1、re.compile(pattern,flags = 0 )</h4>
<p><span><span>将正则表达式模式编译为正则表达式对象,可使用match(),search()以及下面所述的其他方法将其用于匹配</span></span></p>
<div class="cnblogs_code">
<pre>&gt;&gt;&gt; prog = re.compile(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">\d{2}</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">) <span style="color: rgba(0, 128, 0, 1)">#<span style="color: rgba(0, 128, 0, 1)"> 正则对象</span></span>

</span>&gt;&gt;&gt; prog.search(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">12abc</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
</span>&lt;_sre.SRE_Match object; span=(0, 2), match=<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">12</span><span style="color: rgba(128, 0, 0, 1)">'</span>&gt;
&gt;&gt;&gt; prog.search(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">12abc</span><span style="color: rgba(128, 0, 0, 1)">'</span>).group() <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 通过调用group()方法得到匹配的字符串,如果字符串没有匹配,则返回None。</span>
<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">12</span><span style="color: rgba(128, 0, 0, 1)">'</span>

&gt;&gt;&gt; prog.match(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">123abc</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
</span>&lt;_sre.SRE_Match object; span=(0, 2), match=<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">12</span><span style="color: rgba(128, 0, 0, 1)">'</span>&gt;
&gt;&gt;&gt; prog.match(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">123abc</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">).group()
</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">12</span><span style="color: rgba(128, 0, 0, 1)">'</span>
&gt;&gt;&gt;</pre>
</div>
<h4>2、re.search(pattern,string,flags = 0 )</h4>
<p>  扫描字符串以查找正则表达式模式产生匹配项的第一个位置&nbsp;,然后返回相应的<span class="reference internal"><span class="std std-ref">match对象</span></span>。<code class="docutils literal notranslate"><span class="pre">None</span></code>如果字符串中没有位置与模式匹配,则返回;否则返回false。请注意,这与在字符串中的某个点找到零长度匹配不同。</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#在这个字符串进行匹配,</span><span style="color: rgba(0, 128, 0, 1)">只会匹配一个对象</span><br>&gt;&gt;&gt; re.search(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">\w+</span><span style="color: rgba(128, 0, 0, 1)">'</span>,<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">abcde</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">).group()
</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">abcde</span><span style="color: rgba(128, 0, 0, 1)">'</span>
&gt;&gt;&gt; re.search(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">a</span><span style="color: rgba(128, 0, 0, 1)">'</span>,<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">abcde</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">).group()
</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">a</span><span style="color: rgba(128, 0, 0, 1)">'</span>
&gt;&gt;&gt;</pre>
</div>
<h4>3、re.match(pattern,string,flags = 0 )</h4>
<p>如果字符串开头的零个或多个字符与正则表达式模式匹配,则返回相应的匹配对象。None如果字符串与模式不匹配,则返回;否则返回false。请注意,这与零长度匹配不同。</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 同search,不过在字符串开始处进行匹配,只会匹配一个对象</span>
&gt;&gt;&gt; re.match(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">a</span><span style="color: rgba(128, 0, 0, 1)">'</span>,<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">abcade</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">).group()
</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">a</span><span style="color: rgba(128, 0, 0, 1)">'</span>
&gt;&gt;&gt; re.match(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">\w+</span><span style="color: rgba(128, 0, 0, 1)">'</span>,<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">abc123de</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">).group()
</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">abc123de</span><span style="color: rgba(128, 0, 0, 1)">'</span>
&gt;&gt;&gt; re.match(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">\D+</span><span style="color: rgba(128, 0, 0, 1)">'</span>,<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">abc123de</span><span style="color: rgba(128, 0, 0, 1)">'</span>).group() <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">非数字</span>
<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">abc</span><span style="color: rgba(128, 0, 0, 1)">'</span>
&gt;&gt;&gt;</pre>
</div>
<h4>4、re.fullmatch(pattern,string,flags = 0 )</h4>
<p>如果整个字符串与正则表达式模式匹配,则返回相应的match对象。None如果字符串与模式不匹配,则返回;否则返回false。请注意,这与零长度匹配不同。</p>
<div class="cnblogs_code">
<pre>&gt;&gt;&gt; re.fullmatch(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">\w+</span><span style="color: rgba(128, 0, 0, 1)">'</span>,<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">abcade</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">).group()
</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">abcade</span><span style="color: rgba(128, 0, 0, 1)">'</span>
&gt;&gt;&gt; re.fullmatch(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">abcade</span><span style="color: rgba(128, 0, 0, 1)">'</span>,<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">abcade</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">).group()
</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">abcade</span><span style="color: rgba(128, 0, 0, 1)">'</span>
&gt;&gt;&gt;</pre>
</div>
<h4>5、re.split(pattern,string,maxsplit = 0,flags = 0 )</h4>
<p>  通过出现模式来拆分字符串。如果在pattern中使用了捕获括号,那么模式中所有组的文本也将作为结果列表的一部分返回。如果maxsplit不为零,则最多会发生maxsplit分割,并将字符串的其余部分作为列表的最后一个元素返回。</p>
<div class="cnblogs_code">
<pre>&gt;&gt;&gt; re.split(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)"></span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">abcd</span><span style="color: rgba(128, 0, 0, 1)">'</span>) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 先按'a'分割得到''和'bcd',在对''和'bcd'分别按'b'分割</span>
[<span style="color: rgba(128, 0, 0, 1)">''</span>, <span style="color: rgba(128, 0, 0, 1)">''</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">cd</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">]
</span>&gt;&gt;&gt; re.split(r<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">\W+</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">Words, words, words.</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
[</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">Words</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">words</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">words</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">''</span><span style="color: rgba(0, 0, 0, 1)">]
</span>&gt;&gt;&gt; re.split(r<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">(\W+)</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">Words, words, words.</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
[</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">Words</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">, </span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">words</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">, </span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">words</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">.</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">''</span><span style="color: rgba(0, 0, 0, 1)">]
</span>&gt;&gt;&gt; re.split(r<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">\W+</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">Words, words, words.</span><span style="color: rgba(128, 0, 0, 1)">'</span>, 1<span style="color: rgba(0, 0, 0, 1)">)
[</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">Words</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">words, words.</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">]
</span>&gt;&gt;&gt; re.split(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">+</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">0a3B9</span><span style="color: rgba(128, 0, 0, 1)">'</span>, flags=<span style="color: rgba(0, 0, 0, 1)">re.IGNORECASE)
[</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">0</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">3</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">9</span><span style="color: rgba(128, 0, 0, 1)">'</span>]</pre>
</div>
<p><span>如果分隔符中有捕获组,并且该匹配组在字符串的开头匹配,则结果将从空字符串开始。</span><span>字符串的末尾也是如此:</span></p>
<div class="cnblogs_code">
<pre>&gt;&gt;&gt; re.split(r<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">(\W+)</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">...words, words...</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
[</span><span style="color: rgba(128, 0, 0, 1)">''</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">...</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">words</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">, </span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">words</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">...</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">''</span>]</pre>
</div>
<h4>6、re.findall(pattern,string,flags = 0 )</h4>
<p>  以string列表形式返回string中pattern的所有非重叠匹配项。从左到右扫描该字符串,并以找到的顺序返回匹配项。如果该模式中存在一个或多个组,则返回一个组列表;否则,返回一个列表。如果模式包含多个组,则这将是一个元组列表。空匹配项包含在结果中。</p>
<div class="cnblogs_code">
<pre>&gt;&gt;&gt; re.findall(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">a</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">This is a beautiful place!</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
[</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">a</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">a</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">a</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">]
</span>&gt;&gt;&gt;</pre>
</div>
<h4>7、re.finditer(pattern,string,flags = 0 )</h4>
<p>返回一个迭代器,该迭代器在string类型的RE 模式的所有非重叠匹配中产生匹配对象。 从左到右扫描该字符串,并以找到的顺序返回匹配项。空匹配项包含在结果中。</p>
<div class="cnblogs_code">
<pre>&gt;&gt;&gt; re.finditer(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)"></span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">This is a beautiful place!</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
</span>&lt;callable_iterator object at 0x0000000000DCDA90&gt; <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">迭代器对象</span>
&gt;&gt;&gt; ret=re.finditer(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)"></span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">This is a beautiful place!</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
</span>&gt;&gt;&gt; next(ret).group() <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">查看下一个匹配值</span>
<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">a</span><span style="color: rgba(128, 0, 0, 1)">'</span>
&gt;&gt;&gt; <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">查看剩下所有匹配的值</span>
[<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">b</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">a</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">a</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">]
</span>&gt;&gt;&gt;</pre>
</div>
<h4>8、re.sub(pattern,repl,string,count = 0,flags = 0 )</h4>
<p>  返回通过用替换repl替换字符串中最左边的不重叠模式所获得的字符串。如果找不到该模式,&nbsp;则返回的字符串不变。&nbsp;repl可以是字符串或函数;如果是字符串,则处理其中的任何反斜杠转义。即,将其转换为单个换行符,将其转换为回车,依此类推。count参数表示将匹配到的内容进行替换的次数</p>
<div class="cnblogs_code">
<pre>&gt;&gt;&gt; re.sub(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">\d</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">S</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">abc12jh45li78</span><span style="color: rgba(128, 0, 0, 1)">'</span>, 2) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">将匹配到的数字替换成S,替换2个</span>
<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">abcSSjh45li78</span><span style="color: rgba(128, 0, 0, 1)">'</span>

&gt;&gt;&gt; re.sub(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">\d</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">S</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">abc12jh45li78</span><span style="color: rgba(128, 0, 0, 1)">'</span>) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">将匹配到所有的数字替换成S</span>
<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">abcSSjhSSliSS</span><span style="color: rgba(128, 0, 0, 1)">'</span>
&gt;&gt;&gt;</pre>
</div>
<h4>9、re.subn(pattern,repl,string,count = 0,flags = 0 )</h4>
<p>执行与相同的操作sub(),但返回一个元组。(new_string, number_of_subs_made)</p>
<div class="cnblogs_code">
<pre>&gt;&gt;&gt; re.subn(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">\d</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">S</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">abc12jh45li78</span><span style="color: rgba(128, 0, 0, 1)">'</span>, 3<span style="color: rgba(0, 0, 0, 1)">)
(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">abcSSjhS5li78</span><span style="color: rgba(128, 0, 0, 1)">'</span>, 3<span style="color: rgba(0, 0, 0, 1)">)
</span>&gt;&gt;&gt;</pre>
</div>
<h4>10、re.escape(pattern)</h4>
<p><span><span>escape中的所有字符</span></span><em><span><span>图案</span></span></em><span><span>,除了ASCII字母,数字和</span></span><code class="docutils literal notranslate"><span class="pre">'_'</span></code><span><span>。</span><span>如果要匹配可能包含正则表达式元字符的任意文字字符串,这将很有用。</span></span></p>
<div class="cnblogs_code">
<pre>&gt;&gt;&gt; re.escape(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">python.exe\n</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">python\\.exe\\\n</span><span style="color: rgba(128, 0, 0, 1)">'</span>
&gt;&gt;&gt;</pre>
</div>
<h4>11、search()与match()方法</h4>
<p>Python提供了两种基于正则表达式的原始操作: re.match()仅在字符串的开头匹配,re.search()检查匹配项,在字符串中的任何位置检查匹配项(这是Perl的默认设置)。</p>
<div class="cnblogs_code">
<pre>&gt;&gt;&gt; re.match(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">c</span><span style="color: rgba(128, 0, 0, 1)">"</span>, <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">abcdef</span><span style="color: rgba(128, 0, 0, 1)">"</span>) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">Not match</span>
&gt;&gt;&gt; re.search(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">c</span><span style="color: rgba(128, 0, 0, 1)">"</span>, <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">abcdef</span><span style="color: rgba(128, 0, 0, 1)">"</span>) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">match</span>
&lt;_sre.SRE_Match object; span=(2, 3), match=<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">c</span><span style="color: rgba(128, 0, 0, 1)">'</span>&gt;
&gt;&gt;&gt;</pre>
</div>
<p>以开头的正则表达式'^'可用于search()限制字符串开头的匹配项:</p>
<div class="cnblogs_code">
<pre>&gt;&gt;&gt; re.match(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">c</span><span style="color: rgba(128, 0, 0, 1)">"</span>, <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">abcdef</span><span style="color: rgba(128, 0, 0, 1)">"</span>) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">Not match</span>
&gt;&gt;&gt; re.search(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">^c</span><span style="color: rgba(128, 0, 0, 1)">"</span>, <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">abcdef</span><span style="color: rgba(128, 0, 0, 1)">"</span>) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">Not match</span>
&gt;&gt;&gt; re.search(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">^a</span><span style="color: rgba(128, 0, 0, 1)">"</span>, <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">abcdef</span><span style="color: rgba(128, 0, 0, 1)">"</span>) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">match</span>
&lt;_sre.SRE_Match object; span=(0, 1), match=<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">a</span><span style="color: rgba(128, 0, 0, 1)">'</span>&gt;
&gt;&gt;&gt;</pre>
</div>
<p><strong>参考:</strong></p>
<p>https://docs.python.org/3.6/library/re.html</p>
<p>https://www.cnblogs.com/Eva-J/articles/7228075.html#_label7</p>
<p>&nbsp;</p>

</div>
<div id="MySignature" role="contentinfo">
    <div>作者:iveBoy</div>
<div>出处:http://www.cnblogs.com/shenjianping/</div>
<div>本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须在文章页面给出原文连接,否则保留追究法律责任的权利。 </div><br><br>
来源:https://www.cnblogs.com/shenjianping/p/11647473.html
頁: [1]
查看完整版本: Python之re模块