{"id":54,"date":"2021-10-17T14:49:33","date_gmt":"2021-10-17T06:49:33","guid":{"rendered":"http:\/\/gladguang.xyz\/?p=54"},"modified":"2021-10-25T18:35:50","modified_gmt":"2021-10-25T10:35:50","slug":"python-%e6%ad%a3%e5%88%99%e8%a1%a8%e8%be%be%e5%bc%8f","status":"publish","type":"post","link":"https:\/\/www.gladguang.cn\/index.php\/2021\/10\/17\/python-%e6%ad%a3%e5%88%99%e8%a1%a8%e8%be%be%e5%bc%8f\/","title":{"rendered":"Python \u6b63\u5219\u8868\u8fbe\u5f0f"},"content":{"rendered":"\n<p>Python \u6b63\u5219\u8868\u8fbe\u5f0f<br>\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u4e00\u4e2a\u7279\u6b8a\u7684\u5b57\u7b26\u5e8f\u5217\uff0c\u5b83\u80fd\u5e2e\u52a9\u4f60\u65b9\u4fbf\u7684\u68c0\u67e5\u4e00\u4e2a\u5b57\u7b26\u4e32\u662f\u5426\u4e0e\u67d0\u79cd\u6a21\u5f0f\u5339\u914d\u3002<\/p>\n\n\n\n<p>Python \u81ea1.5\u7248\u672c\u8d77\u589e\u52a0\u4e86re \u6a21\u5757\uff0c\u5b83\u63d0\u4f9b Perl \u98ce\u683c\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f\u3002<\/p>\n\n\n\n<p>re \u6a21\u5757\u4f7f Python \u8bed\u8a00\u62e5\u6709\u5168\u90e8\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u529f\u80fd\u3002<\/p>\n\n\n\n<p>compile \u51fd\u6570\u6839\u636e\u4e00\u4e2a\u6a21\u5f0f\u5b57\u7b26\u4e32\u548c\u53ef\u9009\u7684\u6807\u5fd7\u53c2\u6570\u751f\u6210\u4e00\u4e2a\u6b63\u5219\u8868\u8fbe\u5f0f\u5bf9\u8c61\u3002\u8be5\u5bf9\u8c61\u62e5\u6709\u4e00\u7cfb\u5217\u65b9\u6cd5\u7528\u4e8e\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u548c\u66ff\u6362\u3002<\/p>\n\n\n\n<p>re \u6a21\u5757\u4e5f\u63d0\u4f9b\u4e86\u4e0e\u8fd9\u4e9b\u65b9\u6cd5\u529f\u80fd\u5b8c\u5168\u4e00\u81f4\u7684\u51fd\u6570\uff0c\u8fd9\u4e9b\u51fd\u6570\u4f7f\u7528\u4e00\u4e2a\u6a21\u5f0f\u5b57\u7b26\u4e32\u505a\u4e3a\u5b83\u4eec\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u3002<\/p>\n\n\n\n<p>\u672c\u7ae0\u8282\u4e3b\u8981\u4ecb\u7ecdPython\u4e2d\u5e38\u7528\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u5904\u7406\u51fd\u6570\u3002<\/p>\n\n\n\n<p>re.match\u51fd\u6570<br>re.match \u5c1d\u8bd5\u4ece\u5b57\u7b26\u4e32\u7684\u8d77\u59cb\u4f4d\u7f6e\u5339\u914d\u4e00\u4e2a\u6a21\u5f0f\uff0c\u5982\u679c\u4e0d\u662f\u8d77\u59cb\u4f4d\u7f6e\u5339\u914d\u6210\u529f\u7684\u8bdd\uff0cmatch()\u5c31\u8fd4\u56denone\u3002<\/p>\n\n\n\n<p>\u51fd\u6570\u8bed\u6cd5\uff1a<\/p>\n\n\n\n<p>re.match(pattern, string, flags=0)<br>\u51fd\u6570\u53c2\u6570\u8bf4\u660e\uff1a<\/p>\n\n\n\n<p>\u53c2\u6570 \u63cf\u8ff0<br>pattern \u5339\u914d\u7684\u6b63\u5219\u8868\u8fbe\u5f0f<br>string \u8981\u5339\u914d\u7684\u5b57\u7b26\u4e32\u3002<br>flags \u6807\u5fd7\u4f4d\uff0c\u7528\u4e8e\u63a7\u5236\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u5339\u914d\u65b9\u5f0f\uff0c\u5982\uff1a\u662f\u5426\u533a\u5206\u5927\u5c0f\u5199\uff0c\u591a\u884c\u5339\u914d\u7b49\u7b49\u3002\u53c2\u89c1\uff1a\u6b63\u5219\u8868\u8fbe\u5f0f\u4fee\u9970\u7b26 &#8211; \u53ef\u9009\u6807\u5fd7<br>\u5339\u914d\u6210\u529fre.match\u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a\u5339\u914d\u7684\u5bf9\u8c61\uff0c\u5426\u5219\u8fd4\u56deNone\u3002<\/p>\n\n\n\n<p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528group(num) \u6216 groups() \u5339\u914d\u5bf9\u8c61\u51fd\u6570\u6765\u83b7\u53d6\u5339\u914d\u8868\u8fbe\u5f0f\u3002<\/p>\n\n\n\n<p>\u5339\u914d\u5bf9\u8c61\u65b9\u6cd5 \u63cf\u8ff0<br>group(num=0) \u5339\u914d\u7684\u6574\u4e2a\u8868\u8fbe\u5f0f\u7684\u5b57\u7b26\u4e32\uff0cgroup() \u53ef\u4ee5\u4e00\u6b21\u8f93\u5165\u591a\u4e2a\u7ec4\u53f7\uff0c\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\u5b83\u5c06\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u90a3\u4e9b\u7ec4\u6240\u5bf9\u5e94\u503c\u7684\u5143\u7ec4\u3002<br>groups() \u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u6240\u6709\u5c0f\u7ec4\u5b57\u7b26\u4e32\u7684\u5143\u7ec4\uff0c\u4ece 1 \u5230 \u6240\u542b\u7684\u5c0f\u7ec4\u53f7\u3002<br>\u5b9e\u4f8b<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">!\/usr\/bin\/python<\/h1>\n\n\n\n<h1 class=\"wp-block-heading\">&#8211;<em>&#8211; coding: UTF-8 &#8211;<\/em>&#8211;<\/h1>\n\n\n\n<p>import re<br>print(re.match(&#8216;www&#8217;, &#8216;www.runoob.com&#8217;).span()) # \u5728\u8d77\u59cb\u4f4d\u7f6e\u5339\u914d<br>print(re.match(&#8216;com&#8217;, &#8216;www.runoob.com&#8217;)) # \u4e0d\u5728\u8d77\u59cb\u4f4d\u7f6e\u5339\u914d<br>\u4ee5\u4e0a\u5b9e\u4f8b\u8fd0\u884c\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n\n\n\n<p>(0, 3)<br>None<br>\u5b9e\u4f8b<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">!\/usr\/bin\/python<\/h1>\n\n\n\n<p>import re<\/p>\n\n\n\n<p>line = &#8220;Cats are smarter than dogs&#8221;<\/p>\n\n\n\n<p>matchObj = re.match( r'(.<em>) are (.<\/em>?) .*&#8217;, line, re.M|re.I)<\/p>\n\n\n\n<p>if matchObj:<br>print &#8220;matchObj.group() : &#8220;, matchObj.group()<br>print &#8220;matchObj.group(1) : &#8220;, matchObj.group(1)<br>print &#8220;matchObj.group(2) : &#8220;, matchObj.group(2)<br>else:<br>print &#8220;No match!!&#8221;<br>\u4ee5\u4e0a\u5b9e\u4f8b\u6267\u884c\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<p>matchObj.group() : Cats are smarter than dogs<br>matchObj.group(1) : Cats<br>matchObj.group(2) : smarter<br>re.search\u65b9\u6cd5<br>re.search \u626b\u63cf\u6574\u4e2a\u5b57\u7b26\u4e32\u5e76\u8fd4\u56de\u7b2c\u4e00\u4e2a\u6210\u529f\u7684\u5339\u914d\u3002<\/p>\n\n\n\n<p>\u51fd\u6570\u8bed\u6cd5\uff1a<\/p>\n\n\n\n<p>re.search(pattern, string, flags=0)<br>\u51fd\u6570\u53c2\u6570\u8bf4\u660e\uff1a<\/p>\n\n\n\n<p>\u53c2\u6570 \u63cf\u8ff0<br>pattern \u5339\u914d\u7684\u6b63\u5219\u8868\u8fbe\u5f0f<br>string \u8981\u5339\u914d\u7684\u5b57\u7b26\u4e32\u3002<br>flags \u6807\u5fd7\u4f4d\uff0c\u7528\u4e8e\u63a7\u5236\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u5339\u914d\u65b9\u5f0f\uff0c\u5982\uff1a\u662f\u5426\u533a\u5206\u5927\u5c0f\u5199\uff0c\u591a\u884c\u5339\u914d\u7b49\u7b49\u3002<br>\u5339\u914d\u6210\u529fre.search\u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a\u5339\u914d\u7684\u5bf9\u8c61\uff0c\u5426\u5219\u8fd4\u56deNone\u3002<\/p>\n\n\n\n<p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528group(num) \u6216 groups() \u5339\u914d\u5bf9\u8c61\u51fd\u6570\u6765\u83b7\u53d6\u5339\u914d\u8868\u8fbe\u5f0f\u3002<\/p>\n\n\n\n<p>\u5339\u914d\u5bf9\u8c61\u65b9\u6cd5 \u63cf\u8ff0<br>group(num=0) \u5339\u914d\u7684\u6574\u4e2a\u8868\u8fbe\u5f0f\u7684\u5b57\u7b26\u4e32\uff0cgroup() \u53ef\u4ee5\u4e00\u6b21\u8f93\u5165\u591a\u4e2a\u7ec4\u53f7\uff0c\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\u5b83\u5c06\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u90a3\u4e9b\u7ec4\u6240\u5bf9\u5e94\u503c\u7684\u5143\u7ec4\u3002<br>groups() \u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u6240\u6709\u5c0f\u7ec4\u5b57\u7b26\u4e32\u7684\u5143\u7ec4\uff0c\u4ece 1 \u5230 \u6240\u542b\u7684\u5c0f\u7ec4\u53f7\u3002<br>\u5b9e\u4f8b<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">!\/usr\/bin\/python<\/h1>\n\n\n\n<h1 class=\"wp-block-heading\">&#8211;<em>&#8211; coding: UTF-8 &#8211;<\/em>&#8211;<\/h1>\n\n\n\n<p>import re<br>print(re.search(&#8216;www&#8217;, &#8216;www.runoob.com&#8217;).span()) # \u5728\u8d77\u59cb\u4f4d\u7f6e\u5339\u914d<br>print(re.search(&#8216;com&#8217;, &#8216;www.runoob.com&#8217;).span()) # \u4e0d\u5728\u8d77\u59cb\u4f4d\u7f6e\u5339\u914d<br>\u4ee5\u4e0a\u5b9e\u4f8b\u8fd0\u884c\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n\n\n\n<p>(0, 3)<br>(11, 14)<br>\u5b9e\u4f8b<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">!\/usr\/bin\/python<\/h1>\n\n\n\n<p>import re<\/p>\n\n\n\n<p>line = &#8220;Cats are smarter than dogs&#8221;;<\/p>\n\n\n\n<p>searchObj = re.search( r'(.<em>) are (.<\/em>?) .*&#8217;, line, re.M|re.I)<\/p>\n\n\n\n<p>if searchObj:<br>print &#8220;searchObj.group() : &#8220;, searchObj.group()<br>print &#8220;searchObj.group(1) : &#8220;, searchObj.group(1)<br>print &#8220;searchObj.group(2) : &#8220;, searchObj.group(2)<br>else:<br>print &#8220;Nothing found!!&#8221;<br>\u4ee5\u4e0a\u5b9e\u4f8b\u6267\u884c\u7ed3\u679c\u5982\u4e0b\uff1a<br>searchObj.group() : Cats are smarter than dogs<br>searchObj.group(1) : Cats<br>searchObj.group(2) : smarter<br>re.match\u4e0ere.search\u7684\u533a\u522b<br>re.match\u53ea\u5339\u914d\u5b57\u7b26\u4e32\u7684\u5f00\u59cb\uff0c\u5982\u679c\u5b57\u7b26\u4e32\u5f00\u59cb\u4e0d\u7b26\u5408\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u5219\u5339\u914d\u5931\u8d25\uff0c\u51fd\u6570\u8fd4\u56deNone\uff1b\u800cre.search\u5339\u914d\u6574\u4e2a\u5b57\u7b26\u4e32\uff0c\u76f4\u5230\u627e\u5230\u4e00\u4e2a\u5339\u914d\u3002<\/p>\n\n\n\n<p>\u5b9e\u4f8b<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">!\/usr\/bin\/python<\/h1>\n\n\n\n<p>import re<\/p>\n\n\n\n<p>line = &#8220;Cats are smarter than dogs&#8221;;<\/p>\n\n\n\n<p>matchObj = re.match( r&#8217;dogs&#8217;, line, re.M|re.I)<br>if matchObj:<br>print &#8220;match &#8211;&gt; matchObj.group() : &#8220;, matchObj.group()<br>else:<br>print &#8220;No match!!&#8221;<\/p>\n\n\n\n<p>matchObj = re.search( r&#8217;dogs&#8217;, line, re.M|re.I)<br>if matchObj:<br>print &#8220;search &#8211;&gt; searchObj.group() : &#8220;, matchObj.group()<br>else:<br>print &#8220;No match!!&#8221;<br>\u4ee5\u4e0a\u5b9e\u4f8b\u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a<br>No match!!<br>search &#8211;&gt; searchObj.group() : dogs<br>\u68c0\u7d22\u548c\u66ff\u6362<br>Python \u7684 re \u6a21\u5757\u63d0\u4f9b\u4e86re.sub\u7528\u4e8e\u66ff\u6362\u5b57\u7b26\u4e32\u4e2d\u7684\u5339\u914d\u9879\u3002<\/p>\n\n\n\n<p>\u8bed\u6cd5\uff1a<\/p>\n\n\n\n<p>re.sub(pattern, repl, string, count=0, flags=0)<br>\u53c2\u6570\uff1a<\/p>\n\n\n\n<p>pattern : \u6b63\u5219\u4e2d\u7684\u6a21\u5f0f\u5b57\u7b26\u4e32\u3002<br>repl : \u66ff\u6362\u7684\u5b57\u7b26\u4e32\uff0c\u4e5f\u53ef\u4e3a\u4e00\u4e2a\u51fd\u6570\u3002<br>string : \u8981\u88ab\u67e5\u627e\u66ff\u6362\u7684\u539f\u59cb\u5b57\u7b26\u4e32\u3002<br>count : \u6a21\u5f0f\u5339\u914d\u540e\u66ff\u6362\u7684\u6700\u5927\u6b21\u6570\uff0c\u9ed8\u8ba4 0 \u8868\u793a\u66ff\u6362\u6240\u6709\u7684\u5339\u914d\u3002<br>\u5b9e\u4f8b<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">!\/usr\/bin\/python<\/h1>\n\n\n\n<h1 class=\"wp-block-heading\">&#8211;<em>&#8211; coding: UTF-8 &#8211;<\/em>&#8211;<\/h1>\n\n\n\n<p>import re<\/p>\n\n\n\n<p>phone = &#8220;2004-959-559 # \u8fd9\u662f\u4e00\u4e2a\u56fd\u5916\u7535\u8bdd\u53f7\u7801&#8221;<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">\u5220\u9664\u5b57\u7b26\u4e32\u4e2d\u7684 Python\u6ce8\u91ca<\/h1>\n\n\n\n<p>num = re.sub(r&#8217;#.*$&#8217;, &#8220;&#8221;, phone)<br>print &#8220;\u7535\u8bdd\u53f7\u7801\u662f: &#8220;, num<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">\u5220\u9664\u975e\u6570\u5b57(-)\u7684\u5b57\u7b26\u4e32<\/h1>\n\n\n\n<p>num = re.sub(r&#8217;\\D&#8217;, &#8220;&#8221;, phone)<br>print &#8220;\u7535\u8bdd\u53f7\u7801\u662f : &#8220;, num<br>\u4ee5\u4e0a\u5b9e\u4f8b\u6267\u884c\u7ed3\u679c\u5982\u4e0b\uff1a<br>\u7535\u8bdd\u53f7\u7801\u662f: 2004-959-559<br>\u7535\u8bdd\u53f7\u7801\u662f : 2004959559<br>repl \u53c2\u6570\u662f\u4e00\u4e2a\u51fd\u6570<br>\u4ee5\u4e0b\u5b9e\u4f8b\u4e2d\u5c06\u5b57\u7b26\u4e32\u4e2d\u7684\u5339\u914d\u7684\u6570\u5b57\u4e58\u4ee5 2\uff1a<\/p>\n\n\n\n<p>\u5b9e\u4f8b<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">!\/usr\/bin\/python<\/h1>\n\n\n\n<h1 class=\"wp-block-heading\">&#8211;<em>&#8211; coding: UTF-8 &#8211;<\/em>&#8211;<\/h1>\n\n\n\n<p>import re<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">\u5c06\u5339\u914d\u7684\u6570\u5b57\u4e58\u4ee5 2<\/h1>\n\n\n\n<p>def double(matched):<br>value = int(matched.group(&#8216;value&#8217;))<br>return str(value * 2)<\/p>\n\n\n\n<p>s = &#8216;A23G4HFD567&#8217;<br>print(re.sub(&#8216;(?P\\d+)&#8217;, double, s))<br>\u6267\u884c\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n\n\n\n<p>A46G8HFD1134<br>re.compile \u51fd\u6570<br>compile \u51fd\u6570\u7528\u4e8e\u7f16\u8bd1\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u751f\u6210\u4e00\u4e2a\u6b63\u5219\u8868\u8fbe\u5f0f\uff08 Pattern \uff09\u5bf9\u8c61\uff0c\u4f9b match() \u548c search() \u8fd9\u4e24\u4e2a\u51fd\u6570\u4f7f\u7528\u3002<\/p>\n\n\n\n<p>\u8bed\u6cd5\u683c\u5f0f\u4e3a\uff1a<\/p>\n\n\n\n<p>re.compile(pattern[, flags])<br>\u53c2\u6570\uff1a<\/p>\n\n\n\n<p>pattern : \u4e00\u4e2a\u5b57\u7b26\u4e32\u5f62\u5f0f\u7684\u6b63\u5219\u8868\u8fbe\u5f0f<\/p>\n\n\n\n<p>flags : \u53ef\u9009\uff0c\u8868\u793a\u5339\u914d\u6a21\u5f0f\uff0c\u6bd4\u5982\u5ffd\u7565\u5927\u5c0f\u5199\uff0c\u591a\u884c\u6a21\u5f0f\u7b49\uff0c\u5177\u4f53\u53c2\u6570\u4e3a\uff1a<\/p>\n\n\n\n<p>re.I \u5ffd\u7565\u5927\u5c0f\u5199<br>re.L \u8868\u793a\u7279\u6b8a\u5b57\u7b26\u96c6 \\w, \\W, \\b, \\B, \\s, \\S \u4f9d\u8d56\u4e8e\u5f53\u524d\u73af\u5883<br>re.M \u591a\u884c\u6a21\u5f0f<br>re.S \u5373\u4e3a . \u5e76\u4e14\u5305\u62ec\u6362\u884c\u7b26\u5728\u5185\u7684\u4efb\u610f\u5b57\u7b26\uff08. \u4e0d\u5305\u62ec\u6362\u884c\u7b26\uff09<br>re.U \u8868\u793a\u7279\u6b8a\u5b57\u7b26\u96c6 \\w, \\W, \\b, \\B, \\d, \\D, \\s, \\S \u4f9d\u8d56\u4e8e Unicode \u5b57\u7b26\u5c5e\u6027\u6570\u636e\u5e93<br>re.X \u4e3a\u4e86\u589e\u52a0\u53ef\u8bfb\u6027\uff0c\u5ffd\u7565\u7a7a\u683c\u548c # \u540e\u9762\u7684\u6ce8\u91ca<br>\u5b9e\u4f8b<br>\u5b9e\u4f8b<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>import re<br>pattern = re.compile(r&#8217;\\d+&#8217;) # \u7528\u4e8e\u5339\u914d\u81f3\u5c11\u4e00\u4e2a\u6570\u5b57<br>m = pattern.match(&#8216;one12twothree34four&#8217;) # \u67e5\u627e\u5934\u90e8\uff0c\u6ca1\u6709\u5339\u914d<br>print m<br>None<br>m = pattern.match(&#8216;one12twothree34four&#8217;, 2, 10) # \u4ece&#8217;e&#8217;\u7684\u4f4d\u7f6e\u5f00\u59cb\u5339\u914d\uff0c\u6ca1\u6709\u5339\u914d<br>print m<br>None<br>m = pattern.match(&#8216;one12twothree34four&#8217;, 3, 10) # \u4ece&#8217;1&#8217;\u7684\u4f4d\u7f6e\u5f00\u59cb\u5339\u914d\uff0c\u6b63\u597d\u5339\u914d<br>print m # \u8fd4\u56de\u4e00\u4e2a Match \u5bf9\u8c61<br>&lt;_sre.SRE_Match object at 0x10a42aac0&gt;<br>m.group(0) # \u53ef\u7701\u7565 0<br>&#8217;12&#8217;<br>m.start(0) # \u53ef\u7701\u7565 0<br>3<br>m.end(0) # \u53ef\u7701\u7565 0<br>5<br>m.span(0) # \u53ef\u7701\u7565 0<br>(3, 5)<br>\u5728\u4e0a\u9762\uff0c\u5f53\u5339\u914d\u6210\u529f\u65f6\u8fd4\u56de\u4e00\u4e2a Match \u5bf9\u8c61\uff0c\u5176\u4e2d\uff1a<\/p><\/blockquote>\n\n\n\n<p>group([group1, \u2026]) \u65b9\u6cd5\u7528\u4e8e\u83b7\u5f97\u4e00\u4e2a\u6216\u591a\u4e2a\u5206\u7ec4\u5339\u914d\u7684\u5b57\u7b26\u4e32\uff0c\u5f53\u8981\u83b7\u5f97\u6574\u4e2a\u5339\u914d\u7684\u5b50\u4e32\u65f6\uff0c\u53ef\u76f4\u63a5\u4f7f\u7528 group() \u6216 group(0)\uff1b<br>start([group]) \u65b9\u6cd5\u7528\u4e8e\u83b7\u53d6\u5206\u7ec4\u5339\u914d\u7684\u5b50\u4e32\u5728\u6574\u4e2a\u5b57\u7b26\u4e32\u4e2d\u7684\u8d77\u59cb\u4f4d\u7f6e\uff08\u5b50\u4e32\u7b2c\u4e00\u4e2a\u5b57\u7b26\u7684\u7d22\u5f15\uff09\uff0c\u53c2\u6570\u9ed8\u8ba4\u503c\u4e3a 0\uff1b<br>end([group]) \u65b9\u6cd5\u7528\u4e8e\u83b7\u53d6\u5206\u7ec4\u5339\u914d\u7684\u5b50\u4e32\u5728\u6574\u4e2a\u5b57\u7b26\u4e32\u4e2d\u7684\u7ed3\u675f\u4f4d\u7f6e\uff08\u5b50\u4e32\u6700\u540e\u4e00\u4e2a\u5b57\u7b26\u7684\u7d22\u5f15+1\uff09\uff0c\u53c2\u6570\u9ed8\u8ba4\u503c\u4e3a 0\uff1b<br>span([group]) \u65b9\u6cd5\u8fd4\u56de (start(group), end(group))\u3002<br>\u518d\u770b\u770b\u4e00\u4e2a\u4f8b\u5b50\uff1a<\/p>\n\n\n\n<p>\u5b9e\u4f8b<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>import re<br>pattern = re.compile(r'([a-z]+) ([a-z]+)&#8217;, re.I) # re.I \u8868\u793a\u5ffd\u7565\u5927\u5c0f\u5199<br>m = pattern.match(&#8216;Hello World Wide Web&#8217;)<br>print m # \u5339\u914d\u6210\u529f\uff0c\u8fd4\u56de\u4e00\u4e2a Match \u5bf9\u8c61<br>&lt;_sre.SRE_Match object at 0x10bea83e8&gt;<br>m.group(0) # \u8fd4\u56de\u5339\u914d\u6210\u529f\u7684\u6574\u4e2a\u5b50\u4e32<br>&#8216;Hello World&#8217;<br>m.span(0) # \u8fd4\u56de\u5339\u914d\u6210\u529f\u7684\u6574\u4e2a\u5b50\u4e32\u7684\u7d22\u5f15<br>(0, 11)<br>m.group(1) # \u8fd4\u56de\u7b2c\u4e00\u4e2a\u5206\u7ec4\u5339\u914d\u6210\u529f\u7684\u5b50\u4e32<br>&#8216;Hello&#8217;<br>m.span(1) # \u8fd4\u56de\u7b2c\u4e00\u4e2a\u5206\u7ec4\u5339\u914d\u6210\u529f\u7684\u5b50\u4e32\u7684\u7d22\u5f15<br>(0, 5)<br>m.group(2) # \u8fd4\u56de\u7b2c\u4e8c\u4e2a\u5206\u7ec4\u5339\u914d\u6210\u529f\u7684\u5b50\u4e32<br>&#8216;World&#8217;<br>m.span(2) # \u8fd4\u56de\u7b2c\u4e8c\u4e2a\u5206\u7ec4\u5339\u914d\u6210\u529f\u7684\u5b50\u4e32<br>(6, 11)<br>m.groups() # \u7b49\u4ef7\u4e8e (m.group(1), m.group(2), \u2026)<br>(&#8216;Hello&#8217;, &#8216;World&#8217;)<br>m.group(3) # \u4e0d\u5b58\u5728\u7b2c\u4e09\u4e2a\u5206\u7ec4<br>Traceback (most recent call last):<br>File &#8220;&#8221;, line 1, in<br>IndexError: no such group<br>findall<br>\u5728\u5b57\u7b26\u4e32\u4e2d\u627e\u5230\u6b63\u5219\u8868\u8fbe\u5f0f\u6240\u5339\u914d\u7684\u6240\u6709\u5b50\u4e32\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u5217\u8868\uff0c\u5982\u679c\u6ca1\u6709\u627e\u5230\u5339\u914d\u7684\uff0c\u5219\u8fd4\u56de\u7a7a\u5217\u8868\u3002<\/p><\/blockquote>\n\n\n\n<p>\u6ce8\u610f\uff1a match \u548c search \u662f\u5339\u914d\u4e00\u6b21 findall \u5339\u914d\u6240\u6709\u3002<\/p>\n\n\n\n<p>\u8bed\u6cd5\u683c\u5f0f\u4e3a\uff1a<\/p>\n\n\n\n<p>findall(string[, pos[, endpos]])<br>\u53c2\u6570\uff1a<\/p>\n\n\n\n<p>string : \u5f85\u5339\u914d\u7684\u5b57\u7b26\u4e32\u3002<br>pos : \u53ef\u9009\u53c2\u6570\uff0c\u6307\u5b9a\u5b57\u7b26\u4e32\u7684\u8d77\u59cb\u4f4d\u7f6e\uff0c\u9ed8\u8ba4\u4e3a 0\u3002<br>endpos : \u53ef\u9009\u53c2\u6570\uff0c\u6307\u5b9a\u5b57\u7b26\u4e32\u7684\u7ed3\u675f\u4f4d\u7f6e\uff0c\u9ed8\u8ba4\u4e3a\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u3002<br>\u67e5\u627e\u5b57\u7b26\u4e32\u4e2d\u7684\u6240\u6709\u6570\u5b57\uff1a<\/p>\n\n\n\n<p>\u5b9e\u4f8b<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">&#8211;<em>&#8211; coding:UTF8 &#8211;<\/em>&#8211;<\/h1>\n\n\n\n<p>import re<\/p>\n\n\n\n<p>pattern = re.compile(r&#8217;\\d+&#8217;) # \u67e5\u627e\u6570\u5b57<br>result1 = pattern.findall(&#8216;runoob 123 google 456&#8217;)<br>result2 = pattern.findall(&#8216;run88oob123google456&#8217;, 0, 10)<\/p>\n\n\n\n<p>print(result1)<br>print(result2)<br>\u8f93\u51fa\u7ed3\u679c\uff1a<\/p>\n\n\n\n<p>[&#8216;123&#8217;, &#8216;456&#8217;]<br>[&#8217;88&#8217;, &#8217;12&#8217;]<br>re.finditer<br>\u548c findall \u7c7b\u4f3c\uff0c\u5728\u5b57\u7b26\u4e32\u4e2d\u627e\u5230\u6b63\u5219\u8868\u8fbe\u5f0f\u6240\u5339\u914d\u7684\u6240\u6709\u5b50\u4e32\uff0c\u5e76\u628a\u5b83\u4eec\u4f5c\u4e3a\u4e00\u4e2a\u8fed\u4ee3\u5668\u8fd4\u56de\u3002<\/p>\n\n\n\n<p>re.finditer(pattern, string, flags=0)<br>\u53c2\u6570\uff1a<\/p>\n\n\n\n<p>\u53c2\u6570 \u63cf\u8ff0<br>pattern \u5339\u914d\u7684\u6b63\u5219\u8868\u8fbe\u5f0f<br>string \u8981\u5339\u914d\u7684\u5b57\u7b26\u4e32\u3002<br>flags \u6807\u5fd7\u4f4d\uff0c\u7528\u4e8e\u63a7\u5236\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u5339\u914d\u65b9\u5f0f\uff0c\u5982\uff1a\u662f\u5426\u533a\u5206\u5927\u5c0f\u5199\uff0c\u591a\u884c\u5339\u914d\u7b49\u7b49\u3002\u53c2\u89c1\uff1a\u6b63\u5219\u8868\u8fbe\u5f0f\u4fee\u9970\u7b26 &#8211; \u53ef\u9009\u6807\u5fd7<br>\u5b9e\u4f8b<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">&#8211;<em>&#8211; coding: UTF-8 &#8211;<\/em>&#8211;<\/h1>\n\n\n\n<p>import re<\/p>\n\n\n\n<p>it = re.finditer(r&#8221;\\d+&#8221;,&#8221;12a32bc43jf3&#8243;)<br>for match in it:<br>print (match.group() )<br>\u8f93\u51fa\u7ed3\u679c\uff1a<\/p>\n\n\n\n<p>12<br>32<br>43<br>3<br>re.split<br>split \u65b9\u6cd5\u6309\u7167\u80fd\u591f\u5339\u914d\u7684\u5b50\u4e32\u5c06\u5b57\u7b26\u4e32\u5206\u5272\u540e\u8fd4\u56de\u5217\u8868\uff0c\u5b83\u7684\u4f7f\u7528\u5f62\u5f0f\u5982\u4e0b\uff1a<\/p>\n\n\n\n<p>re.split(pattern, string[, maxsplit=0, flags=0])<br>\u53c2\u6570\uff1a<\/p>\n\n\n\n<p>\u53c2\u6570 \u63cf\u8ff0<br>pattern \u5339\u914d\u7684\u6b63\u5219\u8868\u8fbe\u5f0f<br>string \u8981\u5339\u914d\u7684\u5b57\u7b26\u4e32\u3002<br>maxsplit \u5206\u9694\u6b21\u6570\uff0cmaxsplit=1 \u5206\u9694\u4e00\u6b21\uff0c\u9ed8\u8ba4\u4e3a 0\uff0c\u4e0d\u9650\u5236\u6b21\u6570\u3002<br>flags \u6807\u5fd7\u4f4d\uff0c\u7528\u4e8e\u63a7\u5236\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u5339\u914d\u65b9\u5f0f\uff0c\u5982\uff1a\u662f\u5426\u533a\u5206\u5927\u5c0f\u5199\uff0c\u591a\u884c\u5339\u914d\u7b49\u7b49\u3002\u53c2\u89c1\uff1a\u6b63\u5219\u8868\u8fbe\u5f0f\u4fee\u9970\u7b26 &#8211; \u53ef\u9009\u6807\u5fd7<br>\u5b9e\u4f8b<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>import re<br>re.split(&#8216;\\W+&#8217;, &#8216;runoob, runoob, runoob.&#8217;)<br>[&#8216;runoob&#8217;, &#8216;runoob&#8217;, &#8216;runoob&#8217;, &#8221;]<br>re.split(&#8216;(\\W+)&#8217;, &#8216; runoob, runoob, runoob.&#8217;)<br>[&#8221;, &#8216; &#8216;, &#8216;runoob&#8217;, &#8216;, &#8216;, &#8216;runoob&#8217;, &#8216;, &#8216;, &#8216;runoob&#8217;, &#8216;.&#8217;, &#8221;]<br>re.split(&#8216;\\W+&#8217;, &#8216; runoob, runoob, runoob.&#8217;, 1)<br>[&#8221;, &#8216;runoob, runoob, runoob.&#8217;]<\/p><p>re.split(&#8216;a*&#8217;, &#8216;hello world&#8217;) # \u5bf9\u4e8e\u4e00\u4e2a\u627e\u4e0d\u5230\u5339\u914d\u7684\u5b57\u7b26\u4e32\u800c\u8a00\uff0csplit \u4e0d\u4f1a\u5bf9\u5176\u4f5c\u51fa\u5206\u5272<br>[&#8216;hello world&#8217;]<br>\u6b63\u5219\u8868\u8fbe\u5f0f\u5bf9\u8c61<br>re.RegexObject<br>re.compile() \u8fd4\u56de RegexObject \u5bf9\u8c61\u3002<\/p><\/blockquote>\n\n\n\n<p>re.MatchObject<br>group() \u8fd4\u56de\u88ab RE \u5339\u914d\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n\n\n\n<p>start() \u8fd4\u56de\u5339\u914d\u5f00\u59cb\u7684\u4f4d\u7f6e<br>end() \u8fd4\u56de\u5339\u914d\u7ed3\u675f\u7684\u4f4d\u7f6e<br>span() \u8fd4\u56de\u4e00\u4e2a\u5143\u7ec4\u5305\u542b\u5339\u914d (\u5f00\u59cb,\u7ed3\u675f) \u7684\u4f4d\u7f6e<br>\u6b63\u5219\u8868\u8fbe\u5f0f\u4fee\u9970\u7b26 &#8211; \u53ef\u9009\u6807\u5fd7<br>\u6b63\u5219\u8868\u8fbe\u5f0f\u53ef\u4ee5\u5305\u542b\u4e00\u4e9b\u53ef\u9009\u6807\u5fd7\u4fee\u9970\u7b26\u6765\u63a7\u5236\u5339\u914d\u7684\u6a21\u5f0f\u3002\u4fee\u9970\u7b26\u88ab\u6307\u5b9a\u4e3a\u4e00\u4e2a\u53ef\u9009\u7684\u6807\u5fd7\u3002\u591a\u4e2a\u6807\u5fd7\u53ef\u4ee5\u901a\u8fc7\u6309\u4f4d OR(|) \u5b83\u4eec\u6765\u6307\u5b9a\u3002\u5982 re.I | re.M \u88ab\u8bbe\u7f6e\u6210 I \u548c M \u6807\u5fd7\uff1a<\/p>\n\n\n\n<p>\u4fee\u9970\u7b26 \u63cf\u8ff0<br>re.I \u4f7f\u5339\u914d\u5bf9\u5927\u5c0f\u5199\u4e0d\u654f\u611f<br>re.L \u505a\u672c\u5730\u5316\u8bc6\u522b\uff08locale-aware\uff09\u5339\u914d<br>re.M \u591a\u884c\u5339\u914d\uff0c\u5f71\u54cd ^ \u548c $<br>re.S \u4f7f . \u5339\u914d\u5305\u62ec\u6362\u884c\u5728\u5185\u7684\u6240\u6709\u5b57\u7b26<br>re.U \u6839\u636eUnicode\u5b57\u7b26\u96c6\u89e3\u6790\u5b57\u7b26\u3002\u8fd9\u4e2a\u6807\u5fd7\u5f71\u54cd \\w, \\W, \\b, \\B.<br>re.X \u8be5\u6807\u5fd7\u901a\u8fc7\u7ed9\u4e88\u4f60\u66f4\u7075\u6d3b\u7684\u683c\u5f0f\u4ee5\u4fbf\u4f60\u5c06\u6b63\u5219\u8868\u8fbe\u5f0f\u5199\u5f97\u66f4\u6613\u4e8e\u7406\u89e3\u3002<br>\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f<br>\u6a21\u5f0f\u5b57\u7b26\u4e32\u4f7f\u7528\u7279\u6b8a\u7684\u8bed\u6cd5\u6765\u8868\u793a\u4e00\u4e2a\u6b63\u5219\u8868\u8fbe\u5f0f\uff1a<\/p>\n\n\n\n<p>\u5b57\u6bcd\u548c\u6570\u5b57\u8868\u793a\u4ed6\u4eec\u81ea\u8eab\u3002\u4e00\u4e2a\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f\u4e2d\u7684\u5b57\u6bcd\u548c\u6570\u5b57\u5339\u914d\u540c\u6837\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n\n\n\n<p>\u591a\u6570\u5b57\u6bcd\u548c\u6570\u5b57\u524d\u52a0\u4e00\u4e2a\u53cd\u659c\u6760\u65f6\u4f1a\u62e5\u6709\u4e0d\u540c\u7684\u542b\u4e49\u3002<\/p>\n\n\n\n<p>\u6807\u70b9\u7b26\u53f7\u53ea\u6709\u88ab\u8f6c\u4e49\u65f6\u624d\u5339\u914d\u81ea\u8eab\uff0c\u5426\u5219\u5b83\u4eec\u8868\u793a\u7279\u6b8a\u7684\u542b\u4e49\u3002<\/p>\n\n\n\n<p>\u53cd\u659c\u6760\u672c\u8eab\u9700\u8981\u4f7f\u7528\u53cd\u659c\u6760\u8f6c\u4e49\u3002<\/p>\n\n\n\n<p>\u7531\u4e8e\u6b63\u5219\u8868\u8fbe\u5f0f\u901a\u5e38\u90fd\u5305\u542b\u53cd\u659c\u6760\uff0c\u6240\u4ee5\u4f60\u6700\u597d\u4f7f\u7528\u539f\u59cb\u5b57\u7b26\u4e32\u6765\u8868\u793a\u5b83\u4eec\u3002\u6a21\u5f0f\u5143\u7d20(\u5982 r&#8217;\\t&#8217;\uff0c\u7b49\u4ef7\u4e8e &#8216;\\t&#8217;)\u5339\u914d\u76f8\u5e94\u7684\u7279\u6b8a\u5b57\u7b26\u3002<\/p>\n\n\n\n<p>\u4e0b\u8868\u5217\u51fa\u4e86\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f\u8bed\u6cd5\u4e2d\u7684\u7279\u6b8a\u5143\u7d20\u3002\u5982\u679c\u4f60\u4f7f\u7528\u6a21\u5f0f\u7684\u540c\u65f6\u63d0\u4f9b\u4e86\u53ef\u9009\u7684\u6807\u5fd7\u53c2\u6570\uff0c\u67d0\u4e9b\u6a21\u5f0f\u5143\u7d20\u7684\u542b\u4e49\u4f1a\u6539\u53d8\u3002<\/p>\n\n\n\n<p>\u6a21\u5f0f \u63cf\u8ff0<br>^ \u5339\u914d\u5b57\u7b26\u4e32\u7684\u5f00\u5934<br>$ \u5339\u914d\u5b57\u7b26\u4e32\u7684\u672b\u5c3e\u3002<br>. \u5339\u914d\u4efb\u610f\u5b57\u7b26\uff0c\u9664\u4e86\u6362\u884c\u7b26\uff0c\u5f53re.DOTALL\u6807\u8bb0\u88ab\u6307\u5b9a\u65f6\uff0c\u5219\u53ef\u4ee5\u5339\u914d\u5305\u62ec\u6362\u884c\u7b26\u7684\u4efb\u610f\u5b57\u7b26\u3002<br>[\u2026] \u7528\u6765\u8868\u793a\u4e00\u7ec4\u5b57\u7b26,\u5355\u72ec\u5217\u51fa\uff1a[amk] \u5339\u914d &#8216;a&#8217;\uff0c&#8217;m&#8217;\u6216&#8217;k&#8217;<br>[^\u2026] \u4e0d\u5728[]\u4e2d\u7684\u5b57\u7b26\uff1a[^abc] \u5339\u914d\u9664\u4e86a,b,c\u4e4b\u5916\u7684\u5b57\u7b26\u3002<br>re* \u5339\u914d0\u4e2a\u6216\u591a\u4e2a\u7684\u8868\u8fbe\u5f0f\u3002<br>re+ \u5339\u914d1\u4e2a\u6216\u591a\u4e2a\u7684\u8868\u8fbe\u5f0f\u3002<br>re? \u5339\u914d0\u4e2a\u62161\u4e2a\u7531\u524d\u9762\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u5b9a\u4e49\u7684\u7247\u6bb5\uff0c\u975e\u8d2a\u5a6a\u65b9\u5f0f<br>re{ n} \u7cbe\u786e\u5339\u914d n \u4e2a\u524d\u9762\u8868\u8fbe\u5f0f\u3002\u4f8b\u5982\uff0c o{2} \u4e0d\u80fd\u5339\u914d &#8220;Bob&#8221; \u4e2d\u7684 &#8220;o&#8221;\uff0c\u4f46\u662f\u80fd\u5339\u914d &#8220;food&#8221; \u4e2d\u7684\u4e24\u4e2a o\u3002<br>re{ n,} \u5339\u914d n \u4e2a\u524d\u9762\u8868\u8fbe\u5f0f\u3002\u4f8b\u5982\uff0c o{2,} \u4e0d\u80fd\u5339\u914d&#8221;Bob&#8221;\u4e2d\u7684&#8221;o&#8221;\uff0c\u4f46\u80fd\u5339\u914d &#8220;foooood&#8221;\u4e2d\u7684\u6240\u6709 o\u3002&#8221;o{1,}&#8221; \u7b49\u4ef7\u4e8e &#8220;o+&#8221;\u3002&#8221;o{0,}&#8221; \u5219\u7b49\u4ef7\u4e8e &#8220;o*&#8221;\u3002<br>re{ n, m} \u5339\u914d n \u5230 m \u6b21\u7531\u524d\u9762\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u5b9a\u4e49\u7684\u7247\u6bb5\uff0c\u8d2a\u5a6a\u65b9\u5f0f<br>a| b \u5339\u914da\u6216b<br>(re) \u5bf9\u6b63\u5219\u8868\u8fbe\u5f0f\u5206\u7ec4\u5e76\u8bb0\u4f4f\u5339\u914d\u7684\u6587\u672c<br>(?imx) \u6b63\u5219\u8868\u8fbe\u5f0f\u5305\u542b\u4e09\u79cd\u53ef\u9009\u6807\u5fd7\uff1ai, m, \u6216 x \u3002\u53ea\u5f71\u54cd\u62ec\u53f7\u4e2d\u7684\u533a\u57df\u3002<br>(?-imx) \u6b63\u5219\u8868\u8fbe\u5f0f\u5173\u95ed i, m, \u6216 x \u53ef\u9009\u6807\u5fd7\u3002\u53ea\u5f71\u54cd\u62ec\u53f7\u4e2d\u7684\u533a\u57df\u3002<br>(?: re) \u7c7b\u4f3c (\u2026), \u4f46\u662f\u4e0d\u8868\u793a\u4e00\u4e2a\u7ec4<br>(?imx: re) \u5728\u62ec\u53f7\u4e2d\u4f7f\u7528i, m, \u6216 x \u53ef\u9009\u6807\u5fd7<br>(?-imx: re) \u5728\u62ec\u53f7\u4e2d\u4e0d\u4f7f\u7528i, m, \u6216 x \u53ef\u9009\u6807\u5fd7<br>(?#\u2026) \u6ce8\u91ca.<br>(?= re) \u524d\u5411\u80af\u5b9a\u754c\u5b9a\u7b26\u3002\u5982\u679c\u6240\u542b\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u4ee5 \u2026 \u8868\u793a\uff0c\u5728\u5f53\u524d\u4f4d\u7f6e\u6210\u529f\u5339\u914d\u65f6\u6210\u529f\uff0c\u5426\u5219\u5931\u8d25\u3002\u4f46\u4e00\u65e6\u6240\u542b\u8868\u8fbe\u5f0f\u5df2\u7ecf\u5c1d\u8bd5\uff0c\u5339\u914d\u5f15\u64ce\u6839\u672c\u6ca1\u6709\u63d0\u9ad8\uff1b\u6a21\u5f0f\u7684\u5269\u4f59\u90e8\u5206\u8fd8\u8981\u5c1d\u8bd5\u754c\u5b9a\u7b26\u7684\u53f3\u8fb9\u3002<br>(?! re) \u524d\u5411\u5426\u5b9a\u754c\u5b9a\u7b26\u3002\u4e0e\u80af\u5b9a\u754c\u5b9a\u7b26\u76f8\u53cd\uff1b\u5f53\u6240\u542b\u8868\u8fbe\u5f0f\u4e0d\u80fd\u5728\u5b57\u7b26\u4e32\u5f53\u524d\u4f4d\u7f6e\u5339\u914d\u65f6\u6210\u529f<br>(?&gt; re) \u5339\u914d\u7684\u72ec\u7acb\u6a21\u5f0f\uff0c\u7701\u53bb\u56de\u6eaf\u3002<br>\\w \u5339\u914d\u5b57\u6bcd\u6570\u5b57\u53ca\u4e0b\u5212\u7ebf<br>\\W \u5339\u914d\u975e\u5b57\u6bcd\u6570\u5b57\u53ca\u4e0b\u5212\u7ebf<br>\\s \u5339\u914d\u4efb\u610f\u7a7a\u767d\u5b57\u7b26\uff0c\u7b49\u4ef7\u4e8e [ \\t\\n\\r\\f]\u3002<br>\\S \u5339\u914d\u4efb\u610f\u975e\u7a7a\u5b57\u7b26<br>\\d \u5339\u914d\u4efb\u610f\u6570\u5b57\uff0c\u7b49\u4ef7\u4e8e [0-9].<br>\\D \u5339\u914d\u4efb\u610f\u975e\u6570\u5b57<br>\\A \u5339\u914d\u5b57\u7b26\u4e32\u5f00\u59cb<br>\\Z \u5339\u914d\u5b57\u7b26\u4e32\u7ed3\u675f\uff0c\u5982\u679c\u662f\u5b58\u5728\u6362\u884c\uff0c\u53ea\u5339\u914d\u5230\u6362\u884c\u524d\u7684\u7ed3\u675f\u5b57\u7b26\u4e32\u3002<br>\\z \u5339\u914d\u5b57\u7b26\u4e32\u7ed3\u675f<br>\\G \u5339\u914d\u6700\u540e\u5339\u914d\u5b8c\u6210\u7684\u4f4d\u7f6e\u3002<br>\\b \u5339\u914d\u4e00\u4e2a\u5355\u8bcd\u8fb9\u754c\uff0c\u4e5f\u5c31\u662f\u6307\u5355\u8bcd\u548c\u7a7a\u683c\u95f4\u7684\u4f4d\u7f6e\u3002\u4f8b\u5982\uff0c &#8216;er\\b&#8217; \u53ef\u4ee5\u5339\u914d&#8221;never&#8221; \u4e2d\u7684 &#8216;er&#8217;\uff0c\u4f46\u4e0d\u80fd\u5339\u914d &#8220;verb&#8221; \u4e2d\u7684 &#8216;er&#8217;\u3002<br>\\B \u5339\u914d\u975e\u5355\u8bcd\u8fb9\u754c\u3002&#8217;er\\B&#8217; \u80fd\u5339\u914d &#8220;verb&#8221; \u4e2d\u7684 &#8216;er&#8217;\uff0c\u4f46\u4e0d\u80fd\u5339\u914d &#8220;never&#8221; \u4e2d\u7684 &#8216;er&#8217;\u3002<br>\\n, \\t, \u7b49. \u5339\u914d\u4e00\u4e2a\u6362\u884c\u7b26\u3002\u5339\u914d\u4e00\u4e2a\u5236\u8868\u7b26\u3002\u7b49<br>\\1\u2026\\9 \u5339\u914d\u7b2cn\u4e2a\u5206\u7ec4\u7684\u5185\u5bb9\u3002<br>\\10 \u5339\u914d\u7b2cn\u4e2a\u5206\u7ec4\u7684\u5185\u5bb9\uff0c\u5982\u679c\u5b83\u7ecf\u5339\u914d\u3002\u5426\u5219\u6307\u7684\u662f\u516b\u8fdb\u5236\u5b57\u7b26\u7801\u7684\u8868\u8fbe\u5f0f\u3002<br>\u6b63\u5219\u8868\u8fbe\u5f0f\u5b9e\u4f8b<br>\u5b57\u7b26\u5339\u914d<br>\u5b9e\u4f8b \u63cf\u8ff0<br>python \u5339\u914d &#8220;python&#8221;.<br>\u5b57\u7b26\u7c7b<br>\u5b9e\u4f8b \u63cf\u8ff0<br>[Pp]ython \u5339\u914d &#8220;Python&#8221; \u6216 &#8220;python&#8221;<br>rub[ye] \u5339\u914d &#8220;ruby&#8221; \u6216 &#8220;rube&#8221;<br>[aeiou] \u5339\u914d\u4e2d\u62ec\u53f7\u5185\u7684\u4efb\u610f\u4e00\u4e2a\u5b57\u6bcd<br>[0-9] \u5339\u914d\u4efb\u4f55\u6570\u5b57\u3002\u7c7b\u4f3c\u4e8e [0123456789]<br>[a-z] \u5339\u914d\u4efb\u4f55\u5c0f\u5199\u5b57\u6bcd<br>[A-Z] \u5339\u914d\u4efb\u4f55\u5927\u5199\u5b57\u6bcd<br>[a-zA-Z0-9] \u5339\u914d\u4efb\u4f55\u5b57\u6bcd\u53ca\u6570\u5b57<br>[^aeiou] \u9664\u4e86aeiou\u5b57\u6bcd\u4ee5\u5916\u7684\u6240\u6709\u5b57\u7b26<br>[^0-9] \u5339\u914d\u9664\u4e86\u6570\u5b57\u5916\u7684\u5b57\u7b26<br>\u7279\u6b8a\u5b57\u7b26\u7c7b<br>\u5b9e\u4f8b \u63cf\u8ff0<br>. \u5339\u914d\u9664 &#8220;\\n&#8221; \u4e4b\u5916\u7684\u4efb\u4f55\u5355\u4e2a\u5b57\u7b26\u3002\u8981\u5339\u914d\u5305\u62ec &#8216;\\n&#8217; \u5728\u5185\u7684\u4efb\u4f55\u5b57\u7b26\uff0c\u8bf7\u4f7f\u7528\u8c61 &#8216;[.\\n]&#8217; \u7684\u6a21\u5f0f\u3002<br>\\d \u5339\u914d\u4e00\u4e2a\u6570\u5b57\u5b57\u7b26\u3002\u7b49\u4ef7\u4e8e [0-9]\u3002<br>\\D \u5339\u914d\u4e00\u4e2a\u975e\u6570\u5b57\u5b57\u7b26\u3002\u7b49\u4ef7\u4e8e [^0-9]\u3002<br>\\s \u5339\u914d\u4efb\u4f55\u7a7a\u767d\u5b57\u7b26\uff0c\u5305\u62ec\u7a7a\u683c\u3001\u5236\u8868\u7b26\u3001\u6362\u9875\u7b26\u7b49\u7b49\u3002\u7b49\u4ef7\u4e8e [ \\f\\n\\r\\t\\v]\u3002<br>\\S \u5339\u914d\u4efb\u4f55\u975e\u7a7a\u767d\u5b57\u7b26\u3002\u7b49\u4ef7\u4e8e [^ \\f\\n\\r\\t\\v]\u3002<br>\\w \u5339\u914d\u5305\u62ec\u4e0b\u5212\u7ebf\u7684\u4efb\u4f55\u5355\u8bcd\u5b57\u7b26\u3002\u7b49\u4ef7\u4e8e'[A-Za-z0-9_]&#8217;\u3002<br>\\W \u5339\u914d\u4efb\u4f55\u975e\u5355\u8bcd\u5b57\u7b26\u3002\u7b49\u4ef7\u4e8e &#8216;[^A-Za-z0-9_]&#8217;\u3002<br>Python \u9762\u5411\u5bf9\u8c61Python CGI \u7f16\u7a0b<br>2 \u7bc7\u7b14\u8bb0 \u5199\u7b14\u8bb0<br>jim<\/p>\n\n\n\n<p>264***7522@qq.com<\/p>\n\n\n\n<p>528<br>\u6b63\u5219\u8868\u8fbe\u5f0f\u5b9e\u4f8b\uff1a<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">!\/usr\/bin\/python<\/h1>\n\n\n\n<p>import re<br>line = &#8220;Cats are smarter than dogs&#8221;<br>matchObj = re.match( r'(.<em>) are (.<\/em>?) .*&#8217;, line, re.M|re.I)<br>if matchObj:<br>print &#8220;matchObj.group() : &#8220;, matchObj.group()<br>print &#8220;matchObj.group(1) : &#8220;, matchObj.group(1)<br>print &#8220;matchObj.group(2) : &#8220;, matchObj.group(2)<br>else:<br>print &#8220;No match!!&#8221;<br>\u6b63\u5219\u8868\u8fbe\u5f0f\uff1a<\/p>\n\n\n\n<p>r'(.<em>) are (.<\/em>?) .*&#8217;<br>\u89e3\u6790:<\/p>\n\n\n\n<p>\u9996\u5148\uff0c\u8fd9\u662f\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u524d\u9762\u7684\u4e00\u4e2a r \u8868\u793a\u5b57\u7b26\u4e32\u4e3a\u975e\u8f6c\u4e49\u7684\u539f\u59cb\u5b57\u7b26\u4e32\uff0c\u8ba9\u7f16\u8bd1\u5668\u5ffd\u7565\u53cd\u659c\u6760\uff0c\u4e5f\u5c31\u662f\u5ffd\u7565\u8f6c\u4e49\u5b57\u7b26\u3002\u4f46\u662f\u8fd9\u4e2a\u5b57\u7b26\u4e32\u91cc\u6ca1\u6709\u53cd\u659c\u6760\uff0c\u6240\u4ee5\u8fd9\u4e2a r \u53ef\u6709\u53ef\u65e0\u3002<\/p>\n\n\n\n<p>(.<em>) \u7b2c\u4e00\u4e2a\u5339\u914d\u5206\u7ec4\uff0c.<\/em> \u4ee3\u8868\u5339\u914d\u9664\u6362\u884c\u7b26\u4e4b\u5916\u7684\u6240\u6709\u5b57\u7b26\u3002<br>(.<em>?) \u7b2c\u4e8c\u4e2a\u5339\u914d\u5206\u7ec4\uff0c.<\/em>? \u540e\u9762\u591a\u4e2a\u95ee\u53f7\uff0c\u4ee3\u8868\u975e\u8d2a\u5a6a\u6a21\u5f0f\uff0c\u4e5f\u5c31\u662f\u8bf4\u53ea\u5339\u914d\u7b26\u5408\u6761\u4ef6\u7684\u6700\u5c11\u5b57\u7b26<br>\u540e\u9762\u7684\u4e00\u4e2a .* \u6ca1\u6709\u62ec\u53f7\u5305\u56f4\uff0c\u6240\u4ee5\u4e0d\u662f\u5206\u7ec4\uff0c\u5339\u914d\u6548\u679c\u548c\u7b2c\u4e00\u4e2a\u4e00\u6837\uff0c\u4f46\u662f\u4e0d\u8ba1\u5165\u5339\u914d\u7ed3\u679c\u4e2d\u3002<br>matchObj.group() \u7b49\u540c\u4e8e matchObj.group(0)\uff0c\u8868\u793a\u5339\u914d\u5230\u7684\u5b8c\u6574\u6587\u672c\u5b57\u7b26<\/p>\n\n\n\n<p>matchObj.group(1) \u5f97\u5230\u7b2c\u4e00\u7ec4\u5339\u914d\u7ed3\u679c\uff0c\u4e5f\u5c31\u662f(.*)\u5339\u914d\u5230\u7684<\/p>\n\n\n\n<p>matchObj.group(2) \u5f97\u5230\u7b2c\u4e8c\u7ec4\u5339\u914d\u7ed3\u679c\uff0c\u4e5f\u5c31\u662f(.*?)\u5339\u914d\u5230\u7684<\/p>\n\n\n\n<p>\u56e0\u4e3a\u53ea\u6709\u5339\u914d\u7ed3\u679c\u4e2d\u53ea\u6709\u4e24\u7ec4\uff0c\u6240\u4ee5\u5982\u679c\u586b 3 \u65f6\u4f1a\u62a5\u9519\u3002<\/p>\n\n\n\n<p>jim<br>jim<\/p>\n\n\n\n<p>264***7522@qq.com<\/p>\n\n\n\n<p>5\u5e74\u524d (2017-04-24)<br>CrazyDemo<\/p>\n\n\n\n<p>444***899@qq.com<\/p>\n\n\n\n<p>363<br>&#8216;(?P\u2026)&#8217; \u5206\u7ec4\u5339\u914d<\/p>\n\n\n\n<p>\u4f8b\uff1a\u8eab\u4efd\u8bc1 1102231990xxxxxxxx<\/p>\n\n\n\n<p>import re<br>s = &#8216;1102231990xxxxxxxx&#8217;<br>res = re.search(&#8216;(?P\\d{3})(?P\\d{3})(?P\\d{4})&#8217;,s)<br>print(res.groupdict())<br>\u6b64\u5206\u7ec4\u53d6\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n\n\n\n<p>{&#8216;province&#8217;: &#8216;110&#8217;, &#8216;city&#8217;: &#8216;223&#8217;, &#8216;born_year&#8217;: &#8216;1990&#8217;}<br>\u76f4\u63a5\u5c06\u5339\u914d\u7ed3\u679c\u76f4\u63a5\u8f6c\u4e3a\u5b57\u5178\u6a21\u5f0f\uff0c\u65b9\u4fbf\u4f7f\u7528\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python \u6b63\u5219\u8868\u8fbe\u5f0f\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u4e00\u4e2a\u7279\u6b8a\u7684\u5b57\u7b26\u5e8f\u5217\uff0c\u5b83\u80fd\u5e2e\u52a9\u4f60\u65b9\u4fbf\u7684\u68c0\u67e5\u4e00\u4e2a\u5b57\u7b26\u4e32\u662f\u5426\u4e0e\u67d0\u79cd\u6a21\u5f0f\u5339\u914d\u3002 P [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,6],"tags":[23,29,30,32],"class_list":["post-54","post","type-post","status-publish","format-standard","hentry","category-2","category-python","tag-python","tag-29","tag-30","tag-32"],"_links":{"self":[{"href":"https:\/\/www.gladguang.cn\/index.php\/wp-json\/wp\/v2\/posts\/54","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.gladguang.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gladguang.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gladguang.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gladguang.cn\/index.php\/wp-json\/wp\/v2\/comments?post=54"}],"version-history":[{"count":1,"href":"https:\/\/www.gladguang.cn\/index.php\/wp-json\/wp\/v2\/posts\/54\/revisions"}],"predecessor-version":[{"id":178,"href":"https:\/\/www.gladguang.cn\/index.php\/wp-json\/wp\/v2\/posts\/54\/revisions\/178"}],"wp:attachment":[{"href":"https:\/\/www.gladguang.cn\/index.php\/wp-json\/wp\/v2\/media?parent=54"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gladguang.cn\/index.php\/wp-json\/wp\/v2\/categories?post=54"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gladguang.cn\/index.php\/wp-json\/wp\/v2\/tags?post=54"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}