python 中正则表达式用法 re.findall()


参考博客原址:https://blog.csdn.net/YZXnuaa/article/details/79346963

							            <link rel="stylesheet" href="https://csdnimg.cn/release/phoenix/template/css/ck_htmledit_views-5edb848729.css">

					<div class="htmledit_views" id="content_views">
            <pre>正则 re.findall  的简单用法(返回string中所有与pattern相匹配的全部字串,返回形式为数组)

语法:

1
findall(pattern, string, flags = 0 )
import re

Python 正则表达式 re findall 方法能够以列表的形式返回能匹配的子串

print (help(re.findall))

print (dir(re.findall))

findall查找全部r标识代表后面是正则的语句

1
2
3
regular_v1 = re.findall(r "docs" , https://docs.python.org/3/whatsnew/3.6.html )
print (regular_v1)
# [‘docs’]
符号^表示匹配以https开头的的字符串返回,
1
2
3
regular_v2 = re.findall(r "^https" , https://docs.python.org/3/whatsnew/3.6.html )
print (regular_v2)
# [‘https’]

  


     
     
      
      
       
        
         
         
         
         
         
        
          h 
         
        
          t 
         
        
          m 
         
        
          l 
         
         
         
         
         
         
         
         
         
        
          , 
         
         
         
         
         
         
         
         
         
         
         
         
         
         
        
          &lt; 
         
        
          / 
         
        
          p 
         
        
          r 
         
        
          e 
         
        
          &gt; 
         
        
          &lt; 
         
        
          d 
         
        
          i 
         
        
          v 
         
        
          c 
         
        
          l 
         
        
          a 
         
        
          s 
         
        
          s 
         
        
          = 
         
        
          &quot; 
         
        
          c 
         
        
          n 
         
        
          b 
         
        
          l 
         
        
          o 
         
        
          g 
         
         
         
           s 
          
         
           H 
          
         
        
          i 
         
        
          g 
         
        
          h 
         
        
          l 
         
        
          i 
         
        
          g 
         
        
          h 
         
        
          t 
         
        
          e 
         
        
          r 
         
        
          s 
         
        
          h 
         
         
        
          g 
         
        
          u 
         
        
          t 
         
        
          t 
         
        
          e 
         
        
          r 
         
        
          &quot; 
         
        
          &gt; 
         
        
          &lt; 
         
        
          d 
         
        
          i 
         
        
          v 
         
        
          &gt; 
         
        
          &lt; 
         
        
          d 
         
        
          i 
         
        
          v 
         
        
          c 
         
        
          l 
         
        
          a 
         
        
          s 
         
        
          s 
         
        
          = 
         
        
          &quot; 
         
        
          s 
         
        
          y 
         
        
          n 
         
        
          t 
         
        
          a 
         
        
          x 
         
        
          h 
         
        
          i 
         
        
          g 
         
        
          h 
         
        
          l 
         
        
          i 
         
        
          g 
         
        
          h 
         
        
          t 
         
        
          e 
         
        
          r 
         
        
          p 
         
        
          y 
         
        
          t 
         
        
          h 
         
        
          o 
         
        
          n 
         
        
          &quot; 
         
        
          &gt; 
         
        
          &lt; 
         
        
          d 
         
        
          i 
         
        
          v 
         
        
          c 
         
        
          l 
         
        
          a 
         
        
          s 
         
        
          s 
         
        
          = 
         
        
          &quot; 
         
        
          t 
         
        
          a 
         
        
          b 
         
        
          l 
         
        
          e 
         
         
        
          b 
         
        
          o 
         
        
          x 
         
        
          &quot; 
         
        
          &gt; 
         
        
          &lt; 
         
        
          t 
         
        
          a 
         
        
          b 
         
        
          l 
         
        
          e 
         
        
          c 
         
        
          e 
         
        
          l 
         
        
          l 
         
        
          s 
         
        
          p 
         
        
          a 
         
        
          c 
         
        
          i 
         
        
          n 
         
        
          g 
         
        
          = 
         
        
          &quot; 
         
        
          0 
         
        
          &quot; 
         
        
          c 
         
        
          e 
         
        
          l 
         
        
          l 
         
        
          p 
         
        
          a 
         
        
          d 
         
        
          d 
         
        
          i 
         
        
          n 
         
        
          g 
         
        
          = 
         
        
          &quot; 
         
        
          0 
         
        
          &quot; 
         
        
          b 
         
        
          o 
         
        
          r 
         
        
          d 
         
        
          e 
         
        
          r 
         
        
          = 
         
        
          &quot; 
         
        
          0 
         
        
          &quot; 
         
        
          &gt; 
         
        
          &lt; 
         
        
          t 
         
        
          b 
         
        
          o 
         
        
          d 
         
        
          y 
         
        
          &gt; 
         
        
          &lt; 
         
        
          t 
         
        
          r 
         
        
          &gt; 
         
        
          &lt; 
         
        
          t 
         
        
          d 
         
        
          c 
         
        
          l 
         
        
          a 
         
        
          s 
         
        
          s 
         
        
          = 
         
        
          &quot; 
         
        
          g 
         
        
          u 
         
        
          t 
         
        
          t 
         
        
          e 
         
        
          r 
         
        
          &quot; 
         
        
          &gt; 
         
        
          &lt; 
         
        
          d 
         
        
          i 
         
        
          v 
         
        
          c 
         
        
          l 
         
        
          a 
         
        
          s 
         
        
          s 
         
        
          = 
         
        
          &quot; 
         
        
          l 
         
        
          i 
         
        
          n 
         
        
          e 
         
        
          n 
         
        
          u 
         
        
          m 
         
        
          b 
         
        
          e 
         
        
          r 
         
        
          1 
         
        
          i 
         
        
          n 
         
        
          d 
         
        
          e 
         
        
          x 
         
        
          0 
         
        
          a 
         
        
          l 
         
        
          t 
         
        
          2 
         
        
          &quot; 
         
        
          &gt; 
         
        
          1 
         
        
          &lt; 
         
        
          / 
         
        
          d 
         
        
          i 
         
        
          v 
         
        
          &gt; 
         
        
          &lt; 
         
        
          d 
         
        
          i 
         
        
          v 
         
        
          c 
         
        
          l 
         
        
          a 
         
        
          s 
         
        
          s 
         
        
          = 
         
        
          &quot; 
         
        
          l 
         
        
          i 
         
        
          n 
         
        
          e 
         
        
          n 
         
        
          u 
         
        
          m 
         
        
          b 
         
        
          e 
         
        
          r 
         
        
          2 
         
        
          i 
         
        
          n 
         
        
          d 
         
        
          e 
         
        
          x 
         
        
          1 
         
        
          a 
         
        
          l 
         
        
          t 
         
        
          1 
         
        
          &quot; 
         
        
          &gt; 
         
        
          2 
         
        
          &lt; 
         
        
          / 
         
        
          d 
         
        
          i 
         
        
          v 
         
        
          &gt; 
         
        
          &lt; 
         
        
          d 
         
        
          i 
         
        
          v 
         
        
          c 
         
        
          l 
         
        
          a 
         
        
          s 
         
        
          s 
         
        
          = 
         
        
          &quot; 
         
        
          l 
         
        
          i 
         
        
          n 
         
        
          e 
         
        
          n 
         
        
          u 
         
        
          m 
         
        
          b 
         
        
          e 
         
        
          r 
         
        
          3 
         
        
          i 
         
        
          n 
         
        
          d 
         
        
          e 
         
        
          x 
         
        
          2 
         
        
          a 
         
        
          l 
         
        
          t 
         
        
          2 
         
        
          &quot; 
         
        
          &gt; 
         
        
          3 
         
        
          &lt; 
         
        
          / 
         
        
          d 
         
        
          i 
         
        
          v 
         
        
          &gt; 
         
        
          &lt; 
         
        
          / 
         
        
          t 
         
        
          d 
         
        
          &gt; 
         
        
          &lt; 
         
        
          t 
         
        
          d 
         
        
          c 
         
        
          l 
         
        
          a 
         
        
          s 
         
        
          s 
         
        
          = 
         
        
          &quot; 
         
        
          c 
         
        
          o 
         
        
          d 
         
        
          e 
         
        
          &quot; 
         
        
          &gt; 
         
        
          &lt; 
         
        
          d 
         
        
          i 
         
        
          v 
         
        
          &gt; 
         
        
          &lt; 
         
        
          d 
         
        
          i 
         
        
          v 
         
        
          c 
         
        
          l 
         
        
          a 
         
        
          s 
         
        
          s 
         
        
          = 
         
        
          &quot; 
         
        
          l 
         
        
          i 
         
        
          n 
         
        
          e 
         
        
          n 
         
        
          u 
         
        
          m 
         
        
          b 
         
        
          e 
         
        
          r 
         
        
          1 
         
        
          i 
         
        
          n 
         
        
          d 
         
        
          e 
         
        
          x 
         
        
          0 
         
        
          a 
         
        
          l 
         
        
          t 
         
        
          2 
         
        
          &quot; 
         
        
          &gt; 
         
        
          &lt; 
         
        
          c 
         
        
          o 
         
        
          d 
         
        
          e 
         
        
          c 
         
        
          l 
         
        
          a 
         
        
          s 
         
        
          s 
         
        
          = 
         
        
          &quot; 
         
        
          p 
         
        
          y 
         
        
          t 
         
        
          h 
         
        
          o 
         
        
          n 
         
        
          p 
         
        
          l 
         
        
          a 
         
        
          i 
         
        
          n 
         
        
          &quot; 
         
        
          &gt; 
         
        
          r 
         
        
          e 
         
        
          g 
         
        
          u 
         
        
          l 
         
        
          a 
         
         
         
           r 
          
         
           v 
          
         
        
          3 
         
        
          &lt; 
         
        
          / 
         
        
          c 
         
        
          o 
         
        
          d 
         
        
          e 
         
        
          &gt; 
         
        
          &lt; 
         
        
          c 
         
        
          o 
         
        
          d 
         
        
          e 
         
        
          c 
         
        
          l 
         
        
          a 
         
        
          s 
         
        
          s 
         
        
          = 
         
        
          &quot; 
         
        
          p 
         
        
          y 
         
        
          t 
         
        
          h 
         
        
          o 
         
        
          n 
         
        
          k 
         
        
          e 
         
        
          y 
         
        
          w 
         
        
          o 
         
        
          r 
         
        
          d 
         
        
          &quot; 
         
        
          &gt; 
         
        
          = 
         
        
          &lt; 
         
        
          / 
         
        
          c 
         
        
          o 
         
        
          d 
         
        
          e 
         
        
          &gt; 
         
        
          &lt; 
         
        
          c 
         
        
          o 
         
        
          d 
         
        
          e 
         
        
          c 
         
        
          l 
         
        
          a 
         
        
          s 
         
        
          s 
         
        
          = 
         
        
          &quot; 
         
        
          p 
         
        
          y 
         
        
          t 
         
        
          h 
         
        
          o 
         
        
          n 
         
        
          p 
         
        
          l 
         
        
          a 
         
        
          i 
         
        
          n 
         
        
          &quot; 
         
        
          &gt; 
         
        
          r 
         
        
          e 
         
        
          . 
         
        
          f 
         
        
          i 
         
        
          n 
         
        
          d 
         
        
          a 
         
        
          l 
         
        
          l 
         
        
          ( 
         
        
          r 
         
        
          &lt; 
         
        
          / 
         
        
          c 
         
        
          o 
         
        
          d 
         
        
          e 
         
        
          &gt; 
         
        
          &lt; 
         
        
          c 
         
        
          o 
         
        
          d 
         
        
          e 
         
        
          c 
         
        
          l 
         
        
          a 
         
        
          s 
         
        
          s 
         
        
          = 
         
        
          &quot; 
         
        
          p 
         
        
          y 
         
        
          t 
         
        
          h 
         
        
          o 
         
        
          n 
         
        
          s 
         
        
          t 
         
        
          r 
         
        
          i 
         
        
          n 
         
        
          g 
         
        
          &quot; 
         
        
          &gt; 
         
        
          &quot; 
         
        
          h 
         
        
          t 
         
        
          m 
         
        
          l 
         
       
       
       
         符号表示以html结尾的字符串返回,判断是否字符串结束的字符串&lt;/pre&gt;&lt;div class=&quot;cnblogs_Highlighter sh-gutter&quot;&gt;&lt;div&gt;&lt;div class=&quot;syntaxhighlighter python&quot;&gt;&lt;div class=&quot;table-box&quot;&gt;&lt;table cellspacing=&quot;0&quot; cellpadding=&quot;0&quot; border=&quot;0&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;div class=&quot;line number1 index0 alt2&quot;&gt;1&lt;/div&gt;&lt;div class=&quot;line number2 index1 alt1&quot;&gt;2&lt;/div&gt;&lt;div class=&quot;line number3 index2 alt2&quot;&gt;3&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;div&gt;&lt;div class=&quot;line number1 index0 alt2&quot;&gt;&lt;code class=&quot;python plain&quot;&gt;regular_v3 &lt;/code&gt;&lt;code class=&quot;python keyword&quot;&gt;=&lt;/code&gt; &lt;code class=&quot;python plain&quot;&gt;re.findall(r&lt;/code&gt;&lt;code class=&quot;python string&quot;&gt;&quot;html 
       
      
      
     
     ",https://docs.python.org/3/whatsnew/3.6.html)
  
  
print (regular_v3)
# [‘html’]
# […]匹配括号中的其中一个字符
1
2
3
regular_v4 = re.findall(r "[t,w]h" , https://docs.python.org/3/whatsnew/3.6.html )
print (regular_v4)
# [‘th’, ‘wh’]
“d”是正则语法规则用来匹配0到9之间的数返回列表
1
2
3
4
5
6
regular_v5 = re.findall(r "\d" , https://docs.python.org/3/whatsnew/3.6.html )
regular_v6 = re.findall(r "\d\d\d" , https://docs.python.org/3/whatsnew/3.6.html/1234 )
print (regular_v5)
# [‘3’, ‘3’, ‘6’]
print (regular_v6)
# [‘123’]
小d表示取数字0-9,大D表示不要数字,也就是出了数字以外的内容返回
1
2
3
regular_v7 = re.findall(r "\D" , https://docs.python.org/3/whatsnew/3.6.html )
print (regular_v7)
# [‘h’, ‘t’, ‘t’, ‘p’, ‘s’, ‘:’, ‘/’, ‘/’, ‘d’, ‘o’, ‘c’, ‘s’, ‘.’, ‘p’, ‘y’, ‘t’, ‘h’, ‘o’, ‘n’, ‘.’, ‘o’, ‘r’, ‘g’, ‘/’, ‘/’, ‘w’, ‘h’, ‘a’, ‘t’, ‘s’, ‘n’, ‘e’, ‘w’, ‘/’, ‘.’, ‘.’, ‘h’, ‘t’, ‘m’, ‘l’]
“w”在正则里面代表匹配从小写a到z,大写A到Z,数字0到9
1
2
3
regular_v8 = re.findall(r "\w" , https://docs.python.org/3/whatsnew/3.6.html )
print (regular_v8)
#[‘h’, ‘t’, ‘t’, ‘p’, ‘s’, ‘d’, ‘o’, ‘c’, ‘s’, ‘p’, ‘y’, ‘t’, ‘h’, ‘o’, ‘n’, ‘o’, ‘r’, ‘g’, ‘3’, ‘w’, ‘h’, ‘a’, ‘t’, ‘s’, ‘n’, ‘e’, ‘w’, ‘3’, ‘6’, ‘h’, ‘t’, ‘m’, ‘l’]

“W”在正则里面代表匹配除了字母与数字以外的特殊符号
1
2
3
regular_v9 = re.findall(r "\W" , https://docs.python.org/3/whatsnew/3.6.html )
print (regular_v9)
# [’:’, ‘/’, ‘/’, ‘.’, ‘.’, ‘/’, ‘/’, ‘/’, ‘.’, ‘.’]

							            <link rel="stylesheet" href="https://csdnimg.cn/release/phoenix/template/css/ck_htmledit_views-5edb848729.css">

					<div class="htmledit_views" id="content_views">
            <pre>正则 re.findall  的简单用法(返回string中所有与pattern相匹配的全部字串,返回形式为数组)

猜你喜欢

转载自blog.csdn.net/qq_31904559/article/details/85682328