{"id":206,"date":"2023-02-22T22:22:58","date_gmt":"2023-02-22T14:22:58","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=206"},"modified":"2023-04-30T15:28:59","modified_gmt":"2023-04-30T07:28:59","slug":"python-pattern-matching-and-regular-expressions","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/22\/python-pattern-matching-and-regular-expressions\/","title":{"rendered":"Python\u6a21\u5f0f\u5339\u914d\u4e0e\u6b63\u5219\u8868\u8fbe\u5f0f"},"content":{"rendered":"<h3>\u6a21\u5f0f\u5339\u914d\u6d41\u7a0b<\/h3>\n<ol>\n<li>\u7528import re \u5c06\u6b63\u5219\u6a21\u5757\u5bfc\u5165<\/li>\n<li>\u7528re.complie() \u51fd\u6570\u521b\u5efa\u4e00\u4e2aRegex\u5bf9\u8c61\uff08\u8bb0\u5f97\u662f\u4f7f\u7528\u539f\u59cb\u5b57\u7b26\u4e32\uff09<\/li>\n<li>\u5411Regex\u5bf9\u8c61\u7684serach()\uff0c\u65b9\u6cd5\u4f20\u5165\u60f3\u67e5\u627e\u7684\u5b57\u7b26\u4e32<\/li>\n<li>\u8c03\u7528Match\u5bf9\u8c61\u7684group()\uff0c\u8fd4\u56de\u5b9e\u9645\u5339\u914d\u6587\u672c\u7684\u5b57\u7b26\u4e32<\/li>\n<\/ol>\n<p><!-- more --><\/p>\n<h3>\u521b\u5efa\u6b63\u5219\u8868\u8fbe\u5bf9\u8c61<\/h3>\n<p>\u67e5\u627e\u6bb5\u843d\u4e2d\u7684\u7535\u8bdd\u53f7\u7801 \u2018xxx-xxx-xxxx\u2019<\/p>\n<pre><code class=\"language-python\">>&gt;&gt; import re\n>&gt;&gt; phoneNumRegex = re.compile(r&#039;\\d{3}-\\d{3}-\\d{4}&#039;)  #\\d \u8868\u793a\u4e00\u4e2a\u6570\u5b57\u5b57\u7b26\n>&gt;&gt; mo = phoneNumRegex.search(&#039;My number is 123-456-7890&#039;)  #\u5339\u914dRegex\u5bf9\u8c61\n>&gt;&gt; mo.group()\n&#039;123-456-7890&#039;\n>&gt;&gt; print(mo)\n&lt;re.Match object; span=(13, 25), match=&#039;123-456-7890&#039;&gt;<\/code><\/pre>\n<pre><code class=\"language-python\">re.compile(r&#039;\\d{3}-\\d{3}-\\d{4}&#039;).search(&#039;My number is 123-456-7890&#039;).group()<\/code><\/pre>\n<h3>\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u66f4\u591a\u6a21\u5f0f<\/h3>\n<h4>\u5229\u7528\u62ec\u53f7\u5206\u7ec4<\/h4>\n<pre><code class=\"language-python\">>&gt;&gt; phoneNumRegex = re.compile(r&#039;(\\d{3})-(\\d{3}-\\d{4})&#039;)\n>&gt;&gt; mo = phoneNumRegex.search(&#039;My number is 123-456-7890&#039;)\n>&gt;&gt; mo.group()\n&#039;123-456-7890&#039;\n>&gt;&gt; mo.group(1)\n&#039;123&#039;\n>&gt;&gt; mo.group(2)\n&#039;456-7890&#039;\n>&gt;&gt; mo.group(0)\n&#039;123-456-7890&#039;\n>&gt;&gt; mo.groups()\n(&#039;123&#039;, &#039;456-7890&#039;)<\/code><\/pre>\n<p>\u5f53\u4f7f\u7528\u62ec\u53f7\u7684\u65f6\u5019( \u548c) \u6765\u8868\u793a<\/p>\n<pre><code class=\"language-python\">>&gt;&gt; phoneNumRegex = re.compile(r&#039;(\\(\\d{3}\\)) (\\d{3}-\\d{4})&#039;)\n>&gt;&gt; mo = phoneNumRegex.search(&#039;My number is (123) 456-7890&#039;)\n>&gt;&gt; mo\n&lt;re.Match object; span=(13, 27), match=&#039;(123) 456-7890&#039;&gt;\n>&gt;&gt; mo.group()\n&#039;(123) 456-7890&#039;\n>&gt;&gt; mo.groups()\n(&#039;(123)&#039;, &#039;456-7890&#039;)<\/code><\/pre>\n<h4>\u5229\u7528\u7ba1\u9053\u6280\u672f\u5206\u7ec4<\/h4>\n<pre><code class=\"language-python\">>&gt;&gt; heroRegex = re.compile(r&#039;Tom|Job&#039;)\n>&gt;&gt; mo1 = heroRegex.search(&#039;Tom is better Job&#039;)\n>&gt;&gt; mo2 = heroRegex.search(&#039;Job is better Tom&#039;)\n>&gt;&gt; mo1\n&lt;re.Match object; span=(0, 3), match=&#039;Tom&#039;&gt;\n>&gt;&gt; mo1.group()\n&#039;Tom&#039;\n>&gt;&gt; mo2.group()\n&#039;Job&#039;<\/code><\/pre>\n<h4>\u7528 <code>?<\/code> \u5b9e\u73b0\u53ef\u9009\u5206\u914d<\/h4>\n<p>(wo)? \u8868\u793a\u5339\u914d\u65f6\u51fa\u73b0\u96f6\u6b21\u6216\u8005\u4e00\u6b21<\/p>\n<pre><code class=\"language-python\">>&gt;&gt; batRegex = re.compile(r&#039;Bar(wo)?man&#039;)\n>&gt;&gt; temp1 = batRegex.search(&#039;Barman&#039;)\n>&gt;&gt; temp2 = batRegex.search(&#039;Barwoman&#039;)\n>&gt;&gt; temp1.group()\n&#039;Barman&#039;\n>&gt;&gt; temp2.group()\n&#039;Barwoman&#039;\n>&gt;&gt; temp1\n&lt;re.Match object; span=(0, 6), match=&#039;Barman&#039;&gt;\n>&gt;&gt; temp2\n&lt;re.Match object; span=(0, 8), match=&#039;Barwoman&#039;&gt;<\/code><\/pre>\n<h4>\u7528 <code>*<\/code> \u5339\u914d\u96f6\u6b21\u6216\u8005\u591a\u6b21<\/h4>\n<pre><code class=\"language-python\">>&gt;&gt; batRegex = re.compile(r&#039;Bar(wo)*man&#039;)\n>&gt;&gt; temp1 = batRegex.search(&#039;Barman&#039;)\n>&gt;&gt; temp2 = batRegex.search(&#039;Barwoman&#039;)\n>&gt;&gt; temp3 = batRegex.search(&#039;Barwowowowoman&#039;)\n>&gt;&gt; print(temp1,temp2,temp3)\n&lt;re.Match object; span=(0, 6), match=&#039;Barman&#039;&gt;\n&lt;re.Match object; span=(0, 8), match=&#039;Barwoman&#039;&gt; \n&lt;re.Match object; span=(0, 14), match=&#039;Barwowowowoman&#039;&gt;\n>&gt;&gt; print(temp1.group(),temp2.group(),temp3.group())\nBarman Barwoman Barwowowowoman<\/code><\/pre>\n<h4>\u7528 <code>+<\/code> \u8868\u793a\u4e00\u6b21\u6216\u8005\u591a\u6b21<\/h4>\n<pre><code class=\"language-python\">>&gt;&gt; batRegex = re.compile(r&#039;Bar(wo)+man&#039;)\n>&gt;&gt; temp1 = batRegex.search(&#039;Barman&#039;)\n>&gt;&gt; temp2 = batRegex.search(&#039;Barwoman&#039;)\n>&gt;&gt; temp3 = batRegex.search(&#039;Barwowowowoman&#039;)\n>&gt;&gt; print(temp1,temp2,temp3)\nNone  \n#\u56e0\u4e3atemp1\u4e2d\u5bf9\u5e94\u5b57\u7b26\u4e32\u4e2d\u6ca1\u6709\u5339\u914d\u9879\uff0c\u6240\u4ee5\u4e3a None\n&lt;re.Match object; span=(0, 8), match=&#039;Barwoman&#039;&gt; \n&lt;re.Match object; span=(0, 14), match=&#039;Barwowowowoman&#039;&gt;\n>&gt;&gt; print(temp2.group(),temp3.group())\nBarwoman Barwowowowoman<\/code><\/pre>\n<h4>\u4f7f\u7528 <code>{ }<\/code> \u5339\u914d\u7279\u5b9a\u6b21\u6570<\/h4>\n<pre><code>#(Ha){3} &lt;=&gt; #(Ha)(Ha)(Ha)\n(Ha){3,5} &lt;=&gt; ((Ha)(Ha)(Ha))|(Ha)(Ha)(Ha)(Ha)|(Ha)(Ha)(Ha)(Ha)(Ha)<\/code><\/pre>\n<pre><code class=\"language-python\">>&gt;&gt; haRegex = re.compile(r&#039;(ha){3}&#039;)\n>&gt;&gt; mo = haRegex.search(&#039;hahaha&#039;)\n>&gt;&gt; mo.group()\n&#039;hahaha&#039;\n>&gt;&gt; mo.group(0)\n&#039;hahaha&#039;\n>&gt;&gt; mo.group(1)\n&#039;ha&#039;<\/code><\/pre>\n<h3>\u8d2a\u5fc3\u548c\u975e\u8d2a\u5fc3\u5339\u914d<\/h3>\n<pre><code class=\"language-python\">>&gt;&gt; import re\n>&gt;&gt; greedyHaRegex = re.compile(r&#039;(Ha){3,5}&#039;)\n>&gt;&gt; mo1 = greedyHaRegex.search(&#039;HaHaHaHaHa&#039;)\n>&gt;&gt; mo1.group()\n&#039;HaHaHaHaHa&#039;\n>&gt;&gt; greedyHaRegex = re.compile(r&#039;(Ha){3,5}?&#039;)\n>&gt;&gt; mo2 = greedyHaRegex.search(&#039;HaHaHaHaHa&#039;)\n>&gt;&gt; mo2.group()\n&#039;HaHaHa&#039;<\/code><\/pre>\n<h3><code>findall()<\/code> \u65b9\u6cd5<\/h3>\n<p>\u67e5\u627e\u5168\u90e8\u7b26\u5408\u9879<\/p>\n<pre><code class=\"language-python\">>&gt;&gt; phoneNumRegex = re.compile(r&#039;\\d{3}-\\d{3}-\\d{4}&#039;)\n>&gt;&gt; mo = phoneNumRegex.findall(&#039;Cell:415-555-9999 work: 212-555-1000&#039;)\n>&gt;&gt; mo\n[&#039;415-555-9999&#039;, &#039;212-555-1000&#039;]\n>&gt;&gt; phoneNumRegex = re.compile(r&#039;(\\d{3})-(\\d{3})-(\\d{4})&#039;)\n>&gt;&gt; mo = phoneNumRegex.findall(&#039;Cell:415-555-9999 work: 212-555-1000&#039;)\n>&gt;&gt; mo\n[(&#039;415&#039;, &#039;555&#039;, &#039;9999&#039;), (&#039;212&#039;, &#039;555&#039;, &#039;1000&#039;)]<\/code><\/pre>\n<h3>\u5b57\u7b26\u4e32\u5206\u7c7b<\/h3>\n<table>\n<thead>\n<tr>\n<th style=\"text-align: left;\">\u7f29\u5199\u5b57\u7b26\u7c7b\u578b<\/th>\n<th style=\"text-align: center;\">\u8868\u793a<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"text-align: left;\">\\d<\/td>\n<td style=\"text-align: center;\">[0-9]<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">\\D<\/td>\n<td style=\"text-align: center;\">[^0-9]<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">\\w<\/td>\n<td style=\"text-align: center;\">\u4efb\u4f55\u5b57\u6bcd\u6570\u5b57\u6216\u8005\u4e0b\u5212\u7ebf<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">\\W<\/td>\n<td style=\"text-align: center;\">\u9664\u5b57\u6bcd\u6570\u5b57\u6216\u8005\u4e0b\u5212\u7ebf\u7684\u4efb\u4f55\u5b57\u7b26<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">\\s<\/td>\n<td style=\"text-align: center;\">\u7a7a\u683c\u3001\u5236\u8868\u7b26\u3001\u6362\u884c\u7b26<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">\\S<\/td>\n<td style=\"text-align: center;\">\u9664\u7a7a\u683c\u3001\u5236\u8868\u7b26\u3001\u6362\u884c\u7b26\u7684\u4efb\u4f55\u5b57\u7b26<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<pre><code class=\"language-python\">>&gt;&gt; xmasRegex = re.compile(r&#039;\\d+\\s\\w+&#039;)\n>&gt;&gt; mo = xmasRegex.findall(&#039;12 dru, 11 pip, 10 lords, 5 rings&#039;)\n>&gt;&gt; mo\n[&#039;12 dru&#039;, &#039;11 pip&#039;, &#039;10 lords&#039;, &#039;5 rings&#039;]<\/code><\/pre>\n<h3>\u5efa\u7acb\u81ea\u5df1\u7684\u5b57\u7b26\u5206\u7c7b<\/h3>\n<pre><code class=\"language-python\">>&gt;&gt; vowelRegex = re.compile(r&#039;[123abcefg]&#039;)\n>&gt;&gt; mo = vowelRegex.findall(&#039;Hello Word 2018!&#039;)\n>&gt;&gt; mo\n[&#039;e&#039;, &#039;2&#039;, &#039;1&#039;]\n>&gt;&gt; vowelRegex = re.compile(r&#039;[1-5.]&#039;)\n>&gt;&gt; mo = vowelRegex.findall(&#039;Hello Word 2018.&#039;)\n>&gt;&gt; mo\n[&#039;2&#039;, &#039;1&#039;, &#039;.&#039;]<\/code><\/pre>\n<p>\u5728 <code>[]<\/code> \u7684\u524d\u7aef\u52a0\u4e0a <code>^<\/code>\uff0c\u4ee3\u8868\u53d6\u53cd\uff0c\u9664 <code>[]<\/code> \u5185\u5b57\u7b26\u90fd\u53ef\u4ee5\u5339\u914d<\/p>\n<pre><code class=\"language-python\">>&gt;&gt; vowelRegex = re.compile(r&#039;[^123abcefg]&#039;)\n>&gt;&gt; mo = vowelRegex.findall(&#039;Hello Word 2018.&#039;)\n>&gt;&gt; mo\n[&#039;H&#039;, &#039;l&#039;, &#039;l&#039;, &#039;o&#039;, &#039; &#039;, &#039;W&#039;, &#039;o&#039;, &#039;r&#039;, &#039;d&#039;, &#039; &#039;, &#039;0&#039;, &#039;8&#039;, &#039;.&#039;]<\/code><\/pre>\n<h3>\u63d2\u5165\u5b57\u7b26\u548c\u7f8e\u5143\u5b57\u7b26<\/h3>\n<p>\u4e0e\u53d6\u53cd\u4e0d\u540c\uff0c\u8fd9\u91cc\u7684 <code>(r&#039;^xxxx&#039;)<\/code> \u662f\u4ee3\u8868\u5339\u914d\u7684\u5b57\u7b26\u4e32\u9700\u8981\u4ee5\u7279\u5b9a\u7684\u5b57\u7b26\u4e32\u5f00\u5934<\/p>\n<pre><code class=\"language-python\">>&gt;&gt; begRegex = re.compile(r&#039;^hello&#039;)\n>&gt;&gt; mo = begRegex.search(&#039;hello world&#039;)\n>&gt;&gt; mo\n&lt;re.Match object; span=(0, 5), match=&#039;hello&#039;&gt;\n>&gt;&gt; mo.group()\n&#039;hello&#039;<\/code><\/pre>\n<p>\u5982\u679c\u6ca1\u6709\u5339\u914d\u6210\u529f\u7684\u8bdd\uff0c\u8fd4\u56de\u7684\u662f <code>None<\/code><\/p>\n<pre><code class=\"language-python\">>&gt;&gt; begRegex = re.compile(r&#039;^hello&#039;)\n>&gt;&gt; mo = begRegex.search(&#039;hi world&#039;)\n>&gt;&gt; mo == None\nTrue<\/code><\/pre>\n<p>\u5f53\u5339\u914d\u4e3a <code>(r&#039;world$&#039;)<\/code> \u662f\u4ee3\u8868\u5339\u914d\u7684\u5b57\u7b26\u4e32\u9700\u8981\u4ee5\u7279\u5b9a\u7684\u5b57\u7b26\u4e32\u7ed3\u5c3e<\/p>\n<pre><code class=\"language-python\">>&gt;&gt; begRegex = re.compile(r&#039;world$&#039;)\n>&gt;&gt; mo = begRegex.search(&#039;hello world&#039;)\n>&gt;&gt; mo.group()\n&#039;world&#039;<\/code><\/pre>\n<p>\u5f53 <code>(r&#039;^\\d+$&#039;)<\/code> \u662f\u4ee3\u8868\u6574\u4e2a\u5b57\u7b26\u4e32\u90fd\u662f\u6570\u5b57<\/p>\n<pre><code class=\"language-python\">>&gt;&gt; begRegex = re.compile(r&#039;^\\d+$&#039;)\n>&gt;&gt; mo = begRegex.search(&#039;123hello world&#039;)\n>&gt;&gt; mo\n>&gt;&gt; mo == None\nTrue<\/code><\/pre>\n<h3>\u901a\u914d\u5b57\u7b26<\/h3>\n<h4>\u4f7f\u7528 <code>.(\u53e5\u70b9)<\/code> \u5339\u914d <code>\u7a7a\u683c\u53ca\u6362\u884c<\/code> \u4e4b\u5916\u7684\u4efb\u4f55\u5b57\u7b26<\/h4>\n<pre><code class=\"language-python\">>&gt;&gt; atRegex = re.compile(r&#039;.at&#039;)\n>&gt;&gt; atRegex.findall(&#039;The cat in the hat sat on the flat mat.&#039;)\n[&#039;cat&#039;, &#039;hat&#039;, &#039;sat&#039;, &#039;lat&#039;, &#039;mat&#039;]<\/code><\/pre>\n<h4>\u4f7f\u7528 <code>.*<\/code> \u5339\u914d <code>\u6362\u884c<\/code> \u4e4b\u5916\u7684\u4efb\u4f55\u5b57\u7b26<\/h4>\n<pre><code class=\"language-python\">>&gt;&gt; nameRegex = re.compile(r&#039;First Name:(.*) Last Name:(.*)&#039;)\n>&gt;&gt; mo = nameRegex.search(&#039;First Name:AL Last Name:Sweigart&#039;)\n>&gt;&gt; mo.groups()\n(&#039;AL&#039;, &#039;Sweigart&#039;)\n>&gt;&gt; mo\n&lt;re.Match object; span=(0, 32), match=&#039;First Name:AL Last Name:Sweigart&#039;&gt;<\/code><\/pre>\n<h4>\u4f7f\u7528\u53e5\u70b9\u5b57\u7b26\u5339\u914d\u6362\u884c <code>re.DOTALL<\/code><\/h4>\n<pre><code class=\"language-python\">>&gt;&gt; newLineREgex = re.compile(r&#039;.*&#039;, re.DOTALL)\n>&gt;&gt; mo = newLineREgex.search(&quot;Hi:\\nYes,I can do It.\\nThank you.&quot;)\n>&gt;&gt; print(mo.group())\nHi:\nYes,I can do It.\nThank you.\n>&gt;&gt; newLineREgex = re.compile(r&#039;.*&#039;)\n>&gt;&gt; mo = newLineREgex.search(&quot;Hi:\\nYes,I can do It.\\nThank you.&quot;)\n>&gt;&gt; print(mo.group())\nHi:<\/code><\/pre>\n<h3>\u4e0d\u533a\u5206\u5927\u5c0f\u5199\u7684\u5339\u914d <code>re.I or re.IGNORECASE<\/code><\/h3>\n<pre><code class=\"language-python\">>&gt;&gt; robocop = re.compile(r&#039;robocop&#039;, re.I)\n>&gt;&gt; mo = robocop.findall(&#039;RobocoproBOCOPRRROBOCOP&#039;)\n>&gt;&gt; print(mo)\n[&#039;Robocop&#039;, &#039;roBOCOP&#039;, &#039;ROBOCOP&#039;]<\/code><\/pre>\n<h3>\u4f7f\u7528 <code>sub()<\/code> \u65b9\u6cd5\u66ff\u6362\u5b57\u7b26\u4e32<\/h3>\n<pre><code class=\"language-python\">>&gt;&gt; namesRegex = re.compile(r&#039;Agent \\w+&#039;)\n>&gt;&gt; namesRegex.sub(&#039;CENSORED&#039;, &#039;Agent Alice gave the secret documents to Agent Bob&#039;)\n&#039;CENSORED gave the secret documents to CENSORED&#039;<\/code><\/pre>\n<p>\u4fdd\u7559\u524d\u4e00\u4e2a\u5b57\u7b26<\/p>\n<pre><code class=\"language-python\">>&gt;&gt; namesRegex = re.compile(r&#039;Agent (\\w)\\w+&#039;)\n>&gt;&gt; namesRegex.sub(r&#039;\\1****&#039;, &#039;Agent Alice told Agent Carol that Agent &#039;)\n&#039;A**** told C**** that Agent &#039;<\/code><\/pre>\n<p>\u5982\u679c\u60f3\u4fdd\u7559\u4e09\u4e2a\u5b57\u7b26\uff0c\u53ef\u4ee5\u8fd9\u4e48\u505a<\/p>\n<pre><code class=\"language-python\">>&gt;&gt; namesRegex = re.compile(r&#039;Agent (\\w{3})\\w+&#039;)\n>&gt;&gt; namesRegex.sub(r&#039;\\1****&#039;, &#039;Agent Alice told Agent Carol that Agent &#039;)\n&#039;Ali**** told Car**** that Agent &#039;<\/code><\/pre>\n<pre><code class=\"language-python\">>&gt;&gt; namesRegex = re.compile(r&#039;(\\d{3})\\d{4}(\\d{4})&#039;)\n>&gt;&gt; namesRegex.findall(&#039; tell:15735184252;call:13835213493&#039;)\n[(&#039;157&#039;, &#039;4252&#039;), (&#039;138&#039;, &#039;3493&#039;)]\n>&gt;&gt; namesRegex.sub(r&#039;\\1****\\2&#039;, &#039; tell:15735184252;call:13835213493&#039;)\n&#039; tell:157****4252;call:138****3493&#039;<\/code><\/pre>\n<h3>\u7ba1\u7406\u590d\u6742\u7684\u6b63\u5219\u8868\u8fbe\u5f0f<\/h3>\n<p><code>re.VERBOSE<\/code> \u8868\u793a\u53ef\u4ee5\u628a\u6b63\u5219\u8868\u8fbe\u5f0f\u5199\u6210\u591a\u884c\uff0c\u5e76\u4e14\u81ea\u52a8\u5ffd\u7565\u7a7a\u683c\u3002<\/p>\n<pre><code class=\"language-python\">>&gt;&gt; namesRegex = re.compile(r&#039;&#039;&#039;\n(\\d{3}|\\(\\d{3}\\))?       #area code\n(\\s|-|\\.)?               #separator\n\\d{4}                    #fist 4 digits\n(\\s|-|\\.)?               #separator\n\\d{4}                    #end 4 digits)&#039;&#039;&#039;, re.VERBOSE)<\/code><\/pre>\n<h3>\u7ec4\u5408\u4f7f\u7528 <code>re.DOTALL<\/code>\u3001<code>re.VERBOSE<\/code>\u3001<code>re.I<\/code><\/h3>\n<p>\u5229\u7528\u7ba1\u9053\u6280\u672f\uff0c\u89e3\u51b3\u53ea\u6709\u4e00\u4e2a\u503c\u4f5c\u4e3a\u7b2c\u4e8c\u53c2\u6570<\/p>\n<pre><code class=\"language-python\">>&gt;&gt; someRegex = re.compile(&#039;foo&#039;, re.IGNORECASE|re.VERBOSE|re.DOTALL)<\/code><\/pre>\n<h3>\u5c0f\u9879\u76ee\u2014\u2014\u7535\u8bdd\u53f7\u7801\u548cEmail\u63d0\u53d6\u7a0b\u5e8f<\/h3>\n<pre><code class=\"language-python\">#! python3\nimport sys,pyperclip,re\n\nmailRegex = re.compile(r&#039;&#039;&#039;(\n    [a-zA-Z0-9_%+-]+   #username    \n    @                  #\n    [a-zA-Z0-9.-]+     #domain name\n    (\\.[a-zA-Z]{2,4})  #\n    )&#039;&#039;&#039;, re.VERBOSE)\nphoneRegex = re.compile(r&#039;&#039;&#039;\n    \\(?(\\d{3})\\)?\n    (\\s|-|\\.)?\n    (\\d{3})\n    (\\s|-|\\.)\n    (\\d{4})&#039;&#039;&#039;, re.VERBOSE)\n\ntext = str(pyperclip.paste())\nmatches = []\n#123-456-1234\nfor group in phoneRegex.findall(text):\n    phoneNum = &#039;-&#039;.join([group[0],group[2],group[4]])\n    matches.append(phoneNum)\nfor group in mailRegex.findall(text):\n    matches.append(group[0])\n\nif(len(matches) &gt; 0):\n    pyperclip.copy(&#039;\\n&#039;.join(matches))\n    print(&#039;Copied to clipboard:&#039;)\n    print(&#039;\\n&#039;.join(matches))\nelse:\n    print(&quot;No found&quot;)<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u6a21\u5f0f\u5339\u914d\u6d41\u7a0b \u7528import re \u5c06\u6b63\u5219\u6a21\u5757\u5bfc\u5165 \u7528re.complie() \u51fd\u6570\u521b\u5efa\u4e00\u4e2aRegex\u5bf9\u8c61\uff08\u8bb0 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[79],"tags":[],"class_list":["post-206","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/206","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=206"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/206\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=206"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=206"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=206"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}