58664

Python正则表达式中的re.S,re.M,re.I的作用

正则表达式可以包含一些可选标志修饰符来控制匹配的模式。修饰符被指定为一个可选的标志。多个标志可以通过按位 OR(|) 它们来指定。如 re.I | re.M 被设置成 I 和 M 标志:

<table class="reference"><tbody><tr><th>修饰符</th><th>描述</th></tr><tr><td>re.I</td> <td>使匹配对大小写不敏感</td> </tr><tr><td>re.L</td> <td>做本地化识别(locale-aware)匹配</td> </tr><tr><td>re.M</td> <td>多行匹配,影响 ^ 和 $</td> </tr><tr><td>re.S</td> <td>使 . 匹配包括换行在内的所有字符</td> </tr><tr><td>re.U</td> <td>根据Unicode字符集解析字符。这个标志影响 \w, \W, \b, \B.</td> </tr><tr><td>re.X</td> <td>该标志通过给予你更灵活的格式以便你将正则表达式写得更易于理解。</td> </tr></tbody></table><h3>re.S</h3>

在Python的正则表达式中,有一个参数为re.S。它表示 “.” 的作用扩展到整个字符串,包括“\n”。看如下代码:

<table border="0" cellspacing="0" cellpadding="0"><tbody><tr><td class="gutter"> 1 2 3 4 5 6 7 8 </td> <td class="code"> <code class="python keyword">import <code class="python plain">re <code class="python plain">a <code class="python keyword">= <code class="python comments">'''asdfhellopass: <code class="python spaces">    <code class="python comments">worldaf <code class="python spaces">    <code class="python comments">''' <code class="python plain">b <code class="python keyword">= <code class="python plain">re.findall(<code class="python string">'hello(.*?)world'<code class="python plain">,a) <code class="python plain">c <code class="python keyword">= <code class="python plain">re.findall(<code class="python string">'hello(.*?)world'<code class="python plain">,a,re.S) <code class="python functions">print <code class="python string">'b is ' <code class="python plain">, b <code class="python functions">print <code class="python string">'c is ' <code class="python plain">, c </td> </tr></tbody></table>

  

<table border="0" cellspacing="0" cellpadding="0"><tbody><tr><td class="gutter"> 1 2 3 </td> <td class="code"> <code class="python plain">运行结果: <code class="python plain">b <code class="python keyword">is  <code class="python plain">[] <code class="python plain">c <code class="python keyword">is  <code class="python plain">[<code class="python string">'pass:\n\t123\n\t'<code class="python plain">] </td> </tr></tbody></table>

  正则表达式中,“.”的作用是匹配除“\n”以外的任何字符,也就是说,它是在一行中进行匹配。这里的“行”是以“\n”进行区分的。a字符串有每行的末尾有一个“\n”,不过它不可见。

如果不使用re.S参数,则只在每一行内进行匹配,如果一行没有,就换下一行重新开始,不会跨行。而使用re.S参数以后,正则表达式会将这个字符串作为一个整体,将“\n”当做一个普通的字符加入到这个字符串中,在整体中进行匹配。

<h3>re.I</h3>

  不区分大小写

<table border="0" cellspacing="0" cellpadding="0"><tbody><tr><td class="gutter"> 1 2 3 4 5 </td> <td class="code"> <code class="python plain">res <code class="python keyword">= <code class="python plain">re.findall(r<code class="python string">"A"<code class="python plain">, <code class="python string">"abc"<code class="python plain">, re.I) <code class="python functions">print<code class="python plain">(res)     <code class="python plain">运行结果:[<code class="python string">'a'<code class="python plain">] </td> </tr></tbody></table> <h3>re.M</h3>

  将所有行的尾字母输出

<table border="0" cellspacing="0" cellpadding="0"><tbody><tr><td class="gutter"> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 </td> <td class="code"> <code class="python plain">>>> s<code class="python keyword">= <code class="python string">'12 34/n56 78/n90'   <code class="python plain">>>> re.findall( r<code class="python string">'^/d+' <code class="python plain">, s , re.M )          <code class="python comments"># 匹配位于行首的数字   <code class="python plain">[<code class="python string">'12'<code class="python plain">, <code class="python string">'56'<code class="python plain">, <code class="python string">'90'<code class="python plain">]   <code class="python plain">>>> re.findall( r’<code class="python keyword">/<code class="python plain">A<code class="python keyword">/<code class="python plain">d<code class="python keyword">+<code class="python plain">’, s , re.M )        <code class="python comments"># 匹配位于字符串开头的数字   <code class="python plain">[<code class="python string">'12'<code class="python plain">]   <code class="python plain">>>> re.findall( r<code class="python string">'/d+$' <code class="python plain">, s , re.M )          <code class="python comments"># 匹配位于行尾的数字   <code class="python plain">[<code class="python string">'34'<code class="python plain">, <code class="python string">'78'<code class="python plain">, <code class="python string">'90'<code class="python plain">]   <code class="python plain">>>> re.findall( r’<code class="python keyword">/<code class="python plain">d<code class="python keyword">+<code class="python keyword">/<code class="python plain">Z’ , s , re.M )        <code class="python comments"># 匹配位于字符串尾的数字   <code class="python plain">[<code class="python string">'90'<code class="python plain">] </td> </tr></tbody></table>

  

<h3>re.sub</h3> <table border="0" cellspacing="0" cellpadding="0"><tbody><tr><td class="gutter"> 1 2 3 4 </td> <td class="code"> <code class="python comments"># 要求结果:['12', '23', '34'] <code class="python plain">l <code class="python keyword">= <code class="python plain">[<code class="python string">'1 2 '<code class="python plain">, <code class="python string">'2   3'<code class="python plain">, <code class="python string">'  3 4'<code class="python plain">] <code class="python keyword">import <code class="python plain">re <code class="python functions">print<code class="python plain">(<code class="python functions">eval<code class="python plain">(re.sub(r<code class="python string">'\s*'<code class="python plain">, '', <code class="python functions">str<code class="python plain">(l)))) </td> </tr></tbody></table>

来源:博客园

作者:MiaoQinHong

链接:https://www.cnblogs.com/mqhpy/p/11424657.html

Recommend