{"id":989511,"date":"2024-12-27T08:12:13","date_gmt":"2024-12-27T00:12:13","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/989511.html"},"modified":"2024-12-27T08:12:15","modified_gmt":"2024-12-27T00:12:15","slug":"%e5%a6%82%e4%bd%95%e7%90%86%e8%a7%a3python%e6%ad%a3%e5%88%99%e8%a1%a8%e8%be%be","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/989511.html","title":{"rendered":"\u5982\u4f55\u7406\u89e3python\u6b63\u5219\u8868\u8fbe"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25064634\/61cc59c0-03f3-4478-9faf-cba7085ee429.webp\" alt=\"\u5982\u4f55\u7406\u89e3python\u6b63\u5219\u8868\u8fbe\" \/><\/p>\n<p><p> <strong>\u7406\u89e3Python\u6b63\u5219\u8868\u8fbe\u5f0f\u9700\u8981\u638c\u63e1\u5176\u57fa\u672c\u6982\u5ff5\u3001\u5e38\u7528\u6a21\u5f0f\u548c\u51fd\u6570\u3001\u4ee5\u53ca\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u7684\u6280\u5de7\u3002\u901a\u8fc7\u5b66\u4e60\u8fd9\u4e9b\u5185\u5bb9\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u89e3\u6790\u548c\u5904\u7406\u5b57\u7b26\u4e32\u6570\u636e\u3002\u6b63\u5219\u8868\u8fbe\u5f0f\u4ee5\u5176\u5f3a\u5927\u7684\u5b57\u7b26\u4e32\u5904\u7406\u80fd\u529b\u800c\u95fb\u540d\uff0c\u5b83\u5141\u8bb8\u5f00\u53d1\u8005\u901a\u8fc7\u6a21\u5f0f\u5339\u914d\u5feb\u901f\u641c\u7d22\u3001\u66ff\u6362\u548c\u63d0\u53d6\u5b57\u7b26\u4e32\u4e2d\u7684\u7279\u5b9a\u5185\u5bb9\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u7406\u89e3\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u57fa\u672c\u6982\u5ff5\u662f\u638c\u63e1\u5176\u5e94\u7528\u7684\u5173\u952e\u3002\u6b63\u5219\u8868\u8fbe\u5f0f\uff08Regular Expression\uff0c\u7b80\u79f0regex\uff09\u662f\u4e00\u79cd\u63cf\u8ff0\u5b57\u7b26\u6a21\u5f0f\u7684\u7279\u6b8a\u8bed\u6cd5\u3002\u5b83\u53ef\u4ee5\u7528\u6765\u5339\u914d\u5b57\u7b26\u4e32\u4e2d\u7684\u7279\u5b9a\u5b57\u7b26\u7ec4\u5408\u3002Python\u4e2d\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u4e3b\u8981\u901a\u8fc7re\u6a21\u5757\u6765\u5b9e\u73b0\u3002\u901a\u8fc7re\u6a21\u5757\uff0c\u4f60\u53ef\u4ee5\u7f16\u5199\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u641c\u7d22\u3001\u5339\u914d\u548c\u64cd\u4f5c\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u57fa\u7840\u6982\u5ff5\u4e0e\u672f\u8bed<\/h3>\n<\/p>\n<p><p>Python\u4e2d\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u7528\u4e8e\u5b57\u7b26\u4e32\u6a21\u5f0f\u5339\u914d\u548c\u5904\u7406\u3002\u8981\u638c\u63e1\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u9996\u5148\u9700\u8981\u7406\u89e3\u4e00\u4e9b\u57fa\u672c\u672f\u8bed\u548c\u6982\u5ff5\u3002<\/p>\n<\/p>\n<p><h4>1. \u5143\u5b57\u7b26<\/h4>\n<\/p>\n<p><p>\u5143\u5b57\u7b26\u662f\u6b63\u5219\u8868\u8fbe\u5f0f\u4e2d\u7684\u7279\u6b8a\u5b57\u7b26\uff0c\u7528\u4e8e\u5b9a\u4e49\u641c\u7d22\u6a21\u5f0f\u3002\u5e38\u89c1\u7684\u5143\u5b57\u7b26\u5305\u62ec\uff1a<\/p>\n<\/p>\n<ul>\n<li><code>.<\/code>\uff1a\u5339\u914d\u9664\u6362\u884c\u7b26\u4e4b\u5916\u7684\u4efb\u610f\u5b57\u7b26\u3002<\/li>\n<li><code>^<\/code>\uff1a\u5339\u914d\u5b57\u7b26\u4e32\u7684\u5f00\u59cb\u3002<\/li>\n<li><code>$<\/code>\uff1a\u5339\u914d\u5b57\u7b26\u4e32\u7684\u7ed3\u675f\u3002<\/li>\n<li><code>*<\/code>\uff1a\u5339\u914d\u524d\u9762\u7684\u5b57\u7b26\u96f6\u6b21\u6216\u591a\u6b21\u3002<\/li>\n<li><code>+<\/code>\uff1a\u5339\u914d\u524d\u9762\u7684\u5b57\u7b26\u4e00\u6b21\u6216\u591a\u6b21\u3002<\/li>\n<li><code>?<\/code>\uff1a\u5339\u914d\u524d\u9762\u7684\u5b57\u7b26\u96f6\u6b21\u6216\u4e00\u6b21\u3002<\/li>\n<li><code>{n,m}<\/code>\uff1a\u5339\u914d\u524d\u9762\u7684\u5b57\u7b26\u81f3\u5c11n\u6b21\uff0c\u81f3\u591am\u6b21\u3002<\/li>\n<li><code>[]<\/code>\uff1a\u5339\u914d\u62ec\u53f7\u5185\u7684\u4efb\u610f\u5b57\u7b26\u3002<\/li>\n<li><code>|<\/code>\uff1a\u8868\u793a\u201c\u6216\u201d\u64cd\u4f5c\u3002<\/li>\n<li><code>()<\/code>\uff1a\u6807\u8bb0\u4e00\u4e2a\u5b50\u8868\u8fbe\u5f0f\u7684\u5f00\u59cb\u548c\u7ed3\u675f\u4f4d\u7f6e\u3002<\/li>\n<\/ul>\n<p><h4>2. \u8f6c\u4e49\u5b57\u7b26<\/h4>\n<\/p>\n<p><p>\u5728\u6b63\u5219\u8868\u8fbe\u5f0f\u4e2d\uff0c\u5982\u679c\u9700\u8981\u5339\u914d\u5143\u5b57\u7b26\u672c\u8eab\uff0c\u5219\u9700\u8981\u4f7f\u7528\u8f6c\u4e49\u5b57\u7b26\uff08<code><\/code>\uff09\u3002\u4f8b\u5982\uff0c<code>\\.<\/code>\u5339\u914d\u4e00\u4e2a\u70b9\u53f7\uff0c\u800c\u4e0d\u662f\u4efb\u610f\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u5e38\u7528\u51fd\u6570\u548c\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>Python\u7684re\u6a21\u5757\u63d0\u4f9b\u4e86\u591a\u79cd\u51fd\u6570\u548c\u65b9\u6cd5\u6765\u64cd\u4f5c\u6b63\u5219\u8868\u8fbe\u5f0f\u3002<\/p>\n<\/p>\n<p><h4>1. <code>re.match()<\/code><\/h4>\n<\/p>\n<p><p><code>re.match()<\/code>\u4ece\u5b57\u7b26\u4e32\u7684\u8d77\u59cb\u4f4d\u7f6e\u5f00\u59cb\u5339\u914d\u6b63\u5219\u8868\u8fbe\u5f0f\u3002\u53ea\u6709\u5728\u8d77\u59cb\u4f4d\u7f6e\u5339\u914d\u6210\u529f\u65f6\u624d\u8fd4\u56de\u5339\u914d\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>pattern = r&#39;\\d+&#39;<\/p>\n<p>result = re.match(pattern, &#39;123abc&#39;)<\/p>\n<h2><strong>result.group() \u5c06\u8fd4\u56de &#39;123&#39;<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. <code>re.search()<\/code><\/h4>\n<\/p>\n<p><p><code>re.search()<\/code>\u626b\u63cf\u6574\u4e2a\u5b57\u7b26\u4e32\u5e76\u8fd4\u56de\u7b2c\u4e00\u4e2a\u6210\u529f\u5339\u914d\u7684\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>pattern = r&#39;\\d+&#39;<\/p>\n<p>result = re.search(pattern, &#39;abc123def&#39;)<\/p>\n<h2><strong>result.group() \u5c06\u8fd4\u56de &#39;123&#39;<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. <code>re.findall()<\/code><\/h4>\n<\/p>\n<p><p><code>re.findall()<\/code>\u8fd4\u56de\u5b57\u7b26\u4e32\u4e2d\u6240\u6709\u975e\u91cd\u53e0\u5339\u914d\u7684\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>pattern = r&#39;\\d+&#39;<\/p>\n<p>result = re.findall(pattern, &#39;abc123def456&#39;)<\/p>\n<h2><strong>result \u5c06\u662f [&#39;123&#39;, &#39;456&#39;]<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4. <code>re.sub()<\/code><\/h4>\n<\/p>\n<p><p><code>re.sub()<\/code>\u7528\u4e8e\u66ff\u6362\u5b57\u7b26\u4e32\u4e2d\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u7684\u90e8\u5206\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>pattern = r&#39;\\d+&#39;<\/p>\n<p>result = re.sub(pattern, &#39;#&#39;, &#39;abc123def456&#39;)<\/p>\n<h2><strong>result \u5c06\u662f &#39;abc#def#&#39;<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u5e38\u89c1\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f<\/h3>\n<\/p>\n<p><p>\u7406\u89e3\u4e00\u4e9b\u5e38\u89c1\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f\u53ef\u4ee5\u5e2e\u52a9\u4f60\u5feb\u901f\u7f16\u5199\u5339\u914d\u89c4\u5219\u3002<\/p>\n<\/p>\n<p><h4>1. \u5339\u914d\u6570\u5b57<\/h4>\n<\/p>\n<p><p><code>\\d<\/code>\u5339\u914d\u4efb\u610f\u4e00\u4e2a\u6570\u5b57\uff0c<code>\\d+<\/code>\u5339\u914d\u4e00\u4e2a\u6216\u591a\u4e2a\u6570\u5b57\u3002<\/p>\n<\/p>\n<p><h4>2. \u5339\u914d\u5b57\u6bcd<\/h4>\n<\/p>\n<p><p><code>[a-zA-Z]<\/code>\u5339\u914d\u4efb\u610f\u4e00\u4e2a\u5b57\u6bcd\uff0c<code>[a-zA-Z]+<\/code>\u5339\u914d\u4e00\u4e2a\u6216\u591a\u4e2a\u5b57\u6bcd\u3002<\/p>\n<\/p>\n<p><h4>3. \u5339\u914d\u7a7a\u767d\u5b57\u7b26<\/h4>\n<\/p>\n<p><p><code>\\s<\/code>\u5339\u914d\u4efb\u610f\u7a7a\u767d\u5b57\u7b26\uff08\u7a7a\u683c\u3001\u5236\u8868\u7b26\u7b49\uff09\u3002<\/p>\n<\/p>\n<p><h4>4. \u5339\u914d\u7279\u5b9a\u5b57\u7b26\u96c6<\/h4>\n<\/p>\n<p><p><code>[abc]<\/code>\u5339\u914d<code>a<\/code>\u3001<code>b<\/code>\u6216<code>c<\/code>\u4e2d\u7684\u4efb\u610f\u4e00\u4e2a\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u9ad8\u7ea7\u5e94\u7528<\/h3>\n<\/p>\n<p><p>\u638c\u63e1\u4e86\u57fa\u7840\u77e5\u8bc6\u540e\uff0c\u53ef\u4ee5\u5c1d\u8bd5\u4e00\u4e9b\u9ad8\u7ea7\u5e94\u7528\u3002<\/p>\n<\/p>\n<p><h4>1. \u5206\u7ec4\u5339\u914d<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7\u4f7f\u7528\u5706\u62ec\u53f7\u53ef\u4ee5\u5728\u6b63\u5219\u8868\u8fbe\u5f0f\u4e2d\u521b\u5efa\u5206\u7ec4\uff0c\u5e76\u5728\u5339\u914d\u540e\u63d0\u53d6\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>pattern = r&#39;(\\d+)-(\\d+)-(\\d+)&#39;<\/p>\n<p>result = re.match(pattern, &#39;2023-10-07&#39;)<\/p>\n<h2><strong>result.groups() \u5c06\u8fd4\u56de (&#39;2023&#39;, &#39;10&#39;, &#39;07&#39;)<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u975e\u6355\u83b7\u7ec4<\/h4>\n<\/p>\n<p><p>\u975e\u6355\u83b7\u7ec4\u901a\u8fc7<code>(?:...)<\/code>\u8bed\u6cd5\u5b9a\u4e49\uff0c\u5339\u914d\u4f46\u4e0d\u6355\u83b7\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>pattern = r&#39;(?:abc)+&#39;<\/p>\n<p>result = re.match(pattern, &#39;abcabcabc&#39;)<\/p>\n<h2><strong>result.group() \u5c06\u8fd4\u56de &#39;abcabcabc&#39;<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u96f6\u5bbd\u65ad\u8a00<\/h4>\n<\/p>\n<p><p>\u96f6\u5bbd\u65ad\u8a00\u7528\u4e8e\u68c0\u67e5\u67d0\u4e2a\u4f4d\u7f6e\u7684\u524d\u540e\u7279\u5b9a\u5b57\u7b26\uff0c\u800c\u4e0d\u5305\u62ec\u5728\u7ed3\u679c\u4e2d\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u6b63\u5411\u524d\u77bb<\/strong>\uff1a<code>(?=...)<\/code><\/li>\n<li><strong>\u8d1f\u5411\u524d\u77bb<\/strong>\uff1a<code>(?!...)<\/code><\/li>\n<li><strong>\u6b63\u5411\u540e\u987e<\/strong>\uff1a<code>(?&lt;=...)<\/code><\/li>\n<li><strong>\u8d1f\u5411\u540e\u987e<\/strong>\uff1a<code>(?&lt;!...)<\/code><\/li>\n<\/ul>\n<p><h3>\u4e94\u3001\u4f18\u5316\u6b63\u5219\u8868\u8fbe\u5f0f\u6027\u80fd<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u91cf\u6570\u636e\u65f6\uff0c\u4f18\u5316\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u6027\u80fd\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528\u539f\u59cb\u5b57\u7b26\u4e32<\/h4>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u4f7f\u7528\u539f\u59cb\u5b57\u7b26\u4e32\uff08\u4ee5<code>r<\/code>\u5f00\u5934\u7684\u5b57\u7b26\u4e32\uff09\u53ef\u4ee5\u907f\u514d\u8f6c\u4e49\u5b57\u7b26\u7684\u6df7\u6dc6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">pattern = r&#39;\\d+&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u9884\u7f16\u8bd1\u6b63\u5219\u8868\u8fbe\u5f0f<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7<code>re.compile()<\/code>\u53ef\u4ee5\u9884\u7f16\u8bd1\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u4ece\u800c\u63d0\u9ad8\u91cd\u590d\u5339\u914d\u65f6\u7684\u6548\u7387\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>pattern = re.compile(r&#39;\\d+&#39;)<\/p>\n<p>result = pattern.findall(&#39;abc123def456&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u5408\u7406\u4f7f\u7528\u5143\u5b57\u7b26<\/h4>\n<\/p>\n<p><p>\u907f\u514d\u4f7f\u7528\u4e0d\u5fc5\u8981\u7684\u5143\u5b57\u7b26\u548c\u590d\u6742\u7684\u8868\u8fbe\u5f0f\uff0c\u4fdd\u6301\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u7b80\u6d01\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u5b9e\u9645\u5e94\u7528<\/h3>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\u5728\u6570\u636e\u5904\u7406\u4e2d\u6709\u5e7f\u6cdb\u7684\u5e94\u7528\uff0c\u4ee5\u4e0b\u662f\u51e0\u4e2a\u5178\u578b\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h4>1. \u6570\u636e\u9a8c\u8bc1<\/h4>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\u53ef\u4ee5\u7528\u6765\u9a8c\u8bc1\u7535\u5b50\u90ae\u4ef6\u5730\u5740\u3001\u7535\u8bdd\u53f7\u7801\u548c\u90ae\u653f\u7f16\u7801\u7b49\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>def is_valid_em<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>l(email):<\/p>\n<p>    pattern = r&#39;^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$&#39;<\/p>\n<p>    return re.match(pattern, email) is not None<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u6570\u636e\u63d0\u53d6<\/h4>\n<\/p>\n<p><p>\u5728\u7f51\u9875\u6293\u53d6\u4e2d\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f\u5e38\u7528\u4e8e\u4eceHTML\u4e2d\u63d0\u53d6\u7279\u5b9a\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>html_content = &#39;&lt;div&gt;&lt;span&gt;Price: $123&lt;\/span&gt;&lt;\/div&gt;&#39;<\/p>\n<p>pattern = r&#39;Price: \\$(\\d+)&#39;<\/p>\n<p>price = re.search(pattern, html_content).group(1)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u65e5\u5fd7\u5206\u6790<\/h4>\n<\/p>\n<p><p>\u5728\u65e5\u5fd7\u5206\u6790\u4e2d\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f\u53ef\u7528\u4e8e\u89e3\u6790\u548c\u63d0\u53d6\u65e5\u5fd7\u4e2d\u7684\u5173\u952e\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>log_line = &#39;127.0.0.1 - - [10\/Oct\/2023:13:55:36] &quot;GET \/index.html HTTP\/1.1&quot; 200 1043&#39;<\/p>\n<p>pattern = r&#39;(\\d+\\.\\d+\\.\\d+\\.\\d+) - - \\[(.*?)\\] &quot;(.*?)&quot; (\\d+) (\\d+)&#39;<\/p>\n<p>match = re.match(pattern, log_line)<\/p>\n<p>ip_address = match.group(1)<\/p>\n<p>timestamp = match.group(2)<\/p>\n<p>request = match.group(3)<\/p>\n<p>status_code = match.group(4)<\/p>\n<p>response_size = match.group(5)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u8c03\u8bd5\u548c\u6d4b\u8bd5<\/h3>\n<\/p>\n<p><p>\u8c03\u8bd5\u548c\u6d4b\u8bd5\u662f\u786e\u4fdd\u6b63\u5219\u8868\u8fbe\u5f0f\u6b63\u786e\u6027\u7684\u5173\u952e\u6b65\u9aa4\u3002<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528\u5728\u7ebf\u5de5\u5177<\/h4>\n<\/p>\n<p><p>\u6709\u8bb8\u591a\u5728\u7ebf\u5de5\u5177\u53ef\u4ee5\u5e2e\u52a9\u6d4b\u8bd5\u548c\u8c03\u8bd5\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u5982regex101\u548cRegexr\u3002\u8fd9\u4e9b\u5de5\u5177\u63d0\u4f9b\u4e86\u8bed\u6cd5\u9ad8\u4eae\u3001\u5339\u914d\u7ed3\u679c\u53ef\u89c6\u5316\u7b49\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h4>2. \u5355\u5143\u6d4b\u8bd5<\/h4>\n<\/p>\n<p><p>\u4e3a\u6b63\u5219\u8868\u8fbe\u5f0f\u7f16\u5199\u5355\u5143\u6d4b\u8bd5\u53ef\u4ee5\u5e2e\u52a9\u9a8c\u8bc1\u5176\u5728\u4e0d\u540c\u60c5\u51b5\u4e0b\u7684\u884c\u4e3a\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import unittest<\/p>\n<p>import re<\/p>\n<p>class TestRegex(unittest.TestCase):<\/p>\n<p>    def test_email(self):<\/p>\n<p>        pattern = r&#39;^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$&#39;<\/p>\n<p>        self.assertTrue(re.match(pattern, &#39;test@example.com&#39;))<\/p>\n<p>        self.assertFalse(re.match(pattern, &#39;test@com&#39;))<\/p>\n<p>if __name__ == &#39;__main__&#39;:<\/p>\n<p>    unittest.main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u603b\u7ed3\u4e0e\u5efa\u8bae<\/h3>\n<\/p>\n<p><p>\u5728\u5b66\u4e60\u548c\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u65f6\uff0c\u4fdd\u6301\u8010\u5fc3\u548c\u7ec6\u5fc3\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u8bed\u6cd5\u76f8\u5bf9\u590d\u6742\uff0c\u4f46\u4e00\u65e6\u638c\u63e1\uff0c\u4fbf\u80fd\u5927\u5927\u63d0\u9ad8\u6570\u636e\u5904\u7406\u7684\u6548\u7387\u3002\u5efa\u8bae\u521d\u5b66\u8005\u4ece\u7b80\u5355\u7684\u6a21\u5f0f\u5f00\u59cb\uff0c\u9010\u6b65\u589e\u52a0\u590d\u6742\u5ea6\uff0c\u5e76\u591a\u52a0\u7ec3\u4e60\u548c\u5b9e\u9645\u5e94\u7528\u3002\u6b64\u5916\uff0c\u5b9a\u671f\u590d\u4e60\u548c\u66f4\u65b0\u77e5\u8bc6\uff0c\u501f\u52a9\u793e\u533a\u548c\u5728\u7ebf\u8d44\u6e90\uff0c\u53ef\u4ee5\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u5e94\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u4ec0\u4e48\u662f\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u5b83\u5728Python\u4e2d\u6709\u4ec0\u4e48\u7528\uff1f<\/strong><br \/>\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u4e00\u79cd\u7528\u4e8e\u63cf\u8ff0\u5b57\u7b26\u4e32\u6a21\u5f0f\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u7528\u4e8e\u67e5\u627e\u3001\u66ff\u6362\u548c\u9a8c\u8bc1\u5b57\u7b26\u4e32\u5185\u5bb9\u3002\u5728Python\u4e2d\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f\u901a\u8fc7<code>re<\/code>\u6a21\u5757\u8fdb\u884c\u64cd\u4f5c\uff0c\u5e7f\u6cdb\u5e94\u7528\u4e8e\u6570\u636e\u6e05\u6d17\u3001\u6587\u672c\u5904\u7406\u4ee5\u53ca\u590d\u6742\u7684\u5b57\u7b26\u4e32\u5339\u914d\u7b49\u573a\u666f\u3002\u5229\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u9ad8\u6548\u5730\u5904\u7406\u5927\u91cf\u6587\u672c\u6570\u636e\uff0c\u63d0\u9ad8\u4ee3\u7801\u7684\u7075\u6d3b\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u8fdb\u884c\u5b57\u7b26\u4e32\u5339\u914d\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u4f7f\u7528<code>re<\/code>\u6a21\u5757\u4e2d\u7684<code>match()<\/code>\u3001<code>search()<\/code>\u548c<code>findall()<\/code>\u7b49\u51fd\u6570\u53ef\u4ee5\u5b9e\u73b0\u4e0d\u540c\u7c7b\u578b\u7684\u5b57\u7b26\u4e32\u5339\u914d\u3002<code>match()<\/code>\u7528\u4e8e\u4ece\u5b57\u7b26\u4e32\u7684\u8d77\u59cb\u4f4d\u7f6e\u8fdb\u884c\u5339\u914d\uff0c<code>search()<\/code>\u5219\u53ef\u4ee5\u5728\u6574\u4e2a\u5b57\u7b26\u4e32\u4e2d\u641c\u7d22\u5339\u914d\u7684\u6a21\u5f0f\uff0c\u800c<code>findall()<\/code>\u4f1a\u8fd4\u56de\u6240\u6709\u5339\u914d\u7684\u5b50\u4e32\u3002\u4e3a\u4e86\u4f7f\u7528\u8fd9\u4e9b\u529f\u80fd\uff0c\u5f00\u53d1\u8005\u9700\u8981\u5148\u5bfc\u5165<code>re<\/code>\u6a21\u5757\uff0c\u5e76\u5b9a\u4e49\u4e00\u4e2a\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f\u3002<\/p>\n<p><strong>\u6211\u8be5\u5982\u4f55\u8c03\u8bd5\u548c\u4f18\u5316\u6211\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\uff1f<\/strong><br \/>\u8c03\u8bd5\u6b63\u5219\u8868\u8fbe\u5f0f\u53ef\u4ee5\u901a\u8fc7\u5728\u7ebf\u5de5\u5177\u6216Python\u4e2d\u7684<code>re<\/code>\u6a21\u5757\u5b9e\u73b0\u3002\u4f7f\u7528<code>re.compile()<\/code>\u51fd\u6570\u53ef\u4ee5\u5c06\u6b63\u5219\u8868\u8fbe\u5f0f\u7f16\u8bd1\u6210\u4e00\u4e2a\u5bf9\u8c61\uff0c\u4ece\u800c\u63d0\u9ad8\u5339\u914d\u6548\u7387\u3002\u4e3a\u4e86\u4f18\u5316\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u6027\u80fd\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u8003\u8651\u7b80\u5316\u6a21\u5f0f\u3001\u907f\u514d\u4f7f\u7528\u8fc7\u4e8e\u590d\u6742\u7684\u8868\u8fbe\u5f0f\u3001\u4ee5\u53ca\u5408\u7406\u5229\u7528\u91cf\u8bcd\u548c\u5206\u7ec4\u7b49\u6280\u5de7\u3002\u6b64\u5916\uff0c\u4fdd\u6301\u8868\u8fbe\u5f0f\u7684\u53ef\u8bfb\u6027\u4e5f\u975e\u5e38\u91cd\u8981\uff0c\u4ee5\u4fbf\u4e8e\u540e\u671f\u7ef4\u62a4\u548c\u4fee\u6539\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u7406\u89e3Python\u6b63\u5219\u8868\u8fbe\u5f0f\u9700\u8981\u638c\u63e1\u5176\u57fa\u672c\u6982\u5ff5\u3001\u5e38\u7528\u6a21\u5f0f\u548c\u51fd\u6570\u3001\u4ee5\u53ca\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u7684\u6280\u5de7\u3002\u901a\u8fc7\u5b66\u4e60\u8fd9\u4e9b\u5185\u5bb9\uff0c\u53ef\u4ee5\u6709 [&hellip;]","protected":false},"author":3,"featured_media":989522,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/989511"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=989511"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/989511\/revisions"}],"predecessor-version":[{"id":989524,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/989511\/revisions\/989524"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/989522"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=989511"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=989511"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=989511"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}