{"id":1140225,"date":"2025-01-08T22:18:16","date_gmt":"2025-01-08T14:18:16","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1140225.html"},"modified":"2025-01-08T22:18:19","modified_gmt":"2025-01-08T14:18:19","slug":"python%e5%a6%82%e4%bd%95%e7%94%a8%e6%ad%a3%e5%88%99%e8%a1%a8%e8%be%be%e5%bc%8f%e6%8a%93%e5%8f%96%e4%bf%a1%e6%81%af","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1140225.html","title":{"rendered":"python\u5982\u4f55\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6293\u53d6\u4fe1\u606f"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25103304\/bbb69299-0910-4670-adcb-9cde0f335d54.webp\" alt=\"python\u5982\u4f55\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6293\u53d6\u4fe1\u606f\" \/><\/p>\n<p><p> <strong>Python\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6293\u53d6\u4fe1\u606f\u7684\u6838\u5fc3\u8981\u70b9\u6709\uff1a\u4f7f\u7528re\u6a21\u5757\u3001\u5b9a\u4e49\u6b63\u786e\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f\u3001\u4f7f\u7528re.search\u6216re.findall\u51fd\u6570\u3002<\/strong> \u6b63\u5219\u8868\u8fbe\u5f0f\uff08Regular Expressions\uff0c\u7b80\u79f0regex\uff09\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u7528\u4e8e\u6587\u672c\u641c\u7d22\u548c\u5339\u914d\u3002Python\u7684re\u6a21\u5757\u63d0\u4f9b\u4e86\u5bf9\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u652f\u6301\uff0c\u80fd\u591f\u9ad8\u6548\u5730\u5904\u7406\u590d\u6742\u7684\u6587\u672c\u5339\u914d\u4efb\u52a1\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u5c55\u5f00\u5982\u4f55\u4f7f\u7528Python\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u6293\u53d6\u4fe1\u606f\uff0c\u91cd\u70b9\u4ecb\u7ecd\u5982\u4f55\u5b9a\u4e49\u6b63\u5219\u8868\u8fbe\u5f0f\u3001\u4f7f\u7528re\u6a21\u5757\u7684\u57fa\u672c\u65b9\u6cd5\u3001\u4ee5\u53ca\u4e00\u4e9b\u9ad8\u7ea7\u6280\u5de7\u548c\u5b9e\u9645\u5e94\u7528\u3002<\/p>\n<\/p>\n<hr>\n<p><h3>\u4e00\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u57fa\u7840<\/h3>\n<\/p>\n<p><h4>1\u3001\u4ec0\u4e48\u662f\u6b63\u5219\u8868\u8fbe\u5f0f<\/h4>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u4e00\u79cd\u7528\u4e8e\u5339\u914d\u5b57\u7b26\u4e32\u7684\u6a21\u5f0f\u3002\u5b83\u53ef\u4ee5\u7528\u4e8e\u9a8c\u8bc1\u6587\u672c\u683c\u5f0f\u3001\u67e5\u627e\u7279\u5b9a\u6587\u672c\u3001\u66ff\u6362\u6587\u672c\u7b49\u3002\u6b63\u5219\u8868\u8fbe\u5f0f\u7531\u666e\u901a\u5b57\u7b26\u548c\u7279\u6b8a\u5b57\u7b26\uff08\u5143\u5b57\u7b26\uff09\u7ec4\u6210\uff0c\u5143\u5b57\u7b26\u5305\u62ec\u5982<code>^<\/code>\u3001<code>$<\/code>\u3001<code>.<\/code>\u3001<code>*<\/code>\u3001<code>+<\/code>\u3001<code>?<\/code>\u3001<code>[]<\/code>\u3001<code>{}<\/code>\u3001<code>()<\/code>\u7b49\u3002<\/p>\n<\/p>\n<p><h4>2\u3001Python\u4e2d\u7684re\u6a21\u5757<\/h4>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86re\u6a21\u5757\u6765\u652f\u6301\u6b63\u5219\u8868\u8fbe\u5f0f\u3002re\u6a21\u5757\u5305\u542b\u4e86\u8bb8\u591a\u65b9\u6cd5\uff0c\u5982<code>re.match<\/code>\u3001<code>re.search<\/code>\u3001<code>re.findall<\/code>\u3001<code>re.sub<\/code>\u7b49\uff0c\u7528\u4e8e\u4e0d\u540c\u7684\u5339\u914d\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u5b9a\u4e49\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f<\/h3>\n<\/p>\n<p><h4>1\u3001\u57fa\u672c\u5b57\u7b26\u5339\u914d<\/h4>\n<\/p>\n<p><p>\u666e\u901a\u5b57\u7b26\uff1a\u76f4\u63a5\u5339\u914d\u5b57\u7b26\u672c\u8eab\u3002\u4f8b\u5982\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f<code>abc<\/code>\u5339\u914d\u5b57\u7b26\u4e32&quot;abc&quot;\u3002<\/p>\n<\/p>\n<p><p>\u5143\u5b57\u7b26\uff1a\u7279\u6b8a\u542b\u4e49\u7684\u5b57\u7b26\u3002\u4f8b\u5982\uff0c<code>.<\/code>\u5339\u914d\u4efb\u610f\u5355\u4e2a\u5b57\u7b26\uff0c<code>^<\/code>\u5339\u914d\u5b57\u7b26\u4e32\u7684\u5f00\u59cb\uff0c<code>$<\/code>\u5339\u914d\u5b57\u7b26\u4e32\u7684\u7ed3\u5c3e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">pattern = r&quot;abc&quot;<\/p>\n<p>text = &quot;abc&quot;<\/p>\n<p>match = re.match(pattern, text)<\/p>\n<p>print(match)  # \u8f93\u51fa\uff1a&lt;re.Match object; span=(0, 3), match=&#39;abc&#39;&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u5b57\u7b26\u96c6<\/h4>\n<\/p>\n<p><p>\u5b57\u7b26\u96c6\uff1a\u4f7f\u7528\u65b9\u62ec\u53f7<code>[]<\/code>\u5b9a\u4e49\u4e00\u4e2a\u5b57\u7b26\u96c6\uff0c\u5339\u914d\u65b9\u62ec\u53f7\u5185\u7684\u4efb\u610f\u5b57\u7b26\u3002\u4f8b\u5982\uff0c<code>[abc]<\/code>\u5339\u914d&quot;a&quot;\u3001&quot;b&quot;\u6216&quot;c&quot;\u3002<\/p>\n<\/p>\n<p><p>\u8303\u56f4\uff1a\u4f7f\u7528\u8fde\u5b57\u7b26<code>-<\/code>\u5b9a\u4e49\u5b57\u7b26\u8303\u56f4\u3002\u4f8b\u5982\uff0c<code>[a-z]<\/code>\u5339\u914d\u4efb\u610f\u5c0f\u5199\u5b57\u6bcd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">pattern = r&quot;[a-z]&quot;<\/p>\n<p>text = &quot;a&quot;<\/p>\n<p>match = re.match(pattern, text)<\/p>\n<p>print(match)  # \u8f93\u51fa\uff1a&lt;re.Match object; span=(0, 1), match=&#39;a&#39;&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u9884\u5b9a\u4e49\u5b57\u7b26\u96c6<\/h4>\n<\/p>\n<p><p><code>\\d<\/code>\uff1a\u5339\u914d\u4efb\u610f\u6570\u5b57\uff0c\u7b49\u4ef7\u4e8e<code>[0-9]<\/code>\u3002<\/p>\n<\/p>\n<p><p><code>\\w<\/code>\uff1a\u5339\u914d\u4efb\u610f\u5b57\u6bcd\u3001\u6570\u5b57\u548c\u4e0b\u5212\u7ebf\uff0c\u7b49\u4ef7\u4e8e<code>[a-zA-Z0-9_]<\/code>\u3002<\/p>\n<\/p>\n<p><p><code>\\s<\/code>\uff1a\u5339\u914d\u4efb\u610f\u7a7a\u767d\u5b57\u7b26\uff0c\u5305\u62ec\u7a7a\u683c\u3001\u5236\u8868\u7b26\u3001\u6362\u884c\u7b26\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">pattern = r&quot;\\d&quot;<\/p>\n<p>text = &quot;1&quot;<\/p>\n<p>match = re.match(pattern, text)<\/p>\n<p>print(match)  # \u8f93\u51fa\uff1a&lt;re.Match object; span=(0, 1), match=&#39;1&#39;&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528re\u6a21\u5757\u7684\u65b9\u6cd5<\/h3>\n<\/p>\n<p><h4>1\u3001re.match<\/h4>\n<\/p>\n<p><p><code>re.match<\/code>\u51fd\u6570\u7528\u4e8e\u4ece\u5b57\u7b26\u4e32\u7684\u5f00\u5934\u5339\u914d\u6b63\u5219\u8868\u8fbe\u5f0f\u3002\u5982\u679c\u5339\u914d\u6210\u529f\uff0c\u8fd4\u56de\u4e00\u4e2amatch\u5bf9\u8c61\uff0c\u5426\u5219\u8fd4\u56deNone\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">pattern = r&quot;hello&quot;<\/p>\n<p>text = &quot;hello world&quot;<\/p>\n<p>match = re.match(pattern, text)<\/p>\n<p>if match:<\/p>\n<p>    print(&quot;\u5339\u914d\u6210\u529f&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;\u5339\u914d\u5931\u8d25&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001re.search<\/h4>\n<\/p>\n<p><p><code>re.search<\/code>\u51fd\u6570\u7528\u4e8e\u5728\u6574\u4e2a\u5b57\u7b26\u4e32\u4e2d\u641c\u7d22\u5339\u914d\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u7b2c\u4e00\u4e2a\u4f4d\u7f6e\u3002\u5982\u679c\u5339\u914d\u6210\u529f\uff0c\u8fd4\u56de\u4e00\u4e2amatch\u5bf9\u8c61\uff0c\u5426\u5219\u8fd4\u56deNone\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">pattern = r&quot;world&quot;<\/p>\n<p>text = &quot;hello world&quot;<\/p>\n<p>match = re.search(pattern, text)<\/p>\n<p>if match:<\/p>\n<p>    print(&quot;\u5339\u914d\u6210\u529f&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;\u5339\u914d\u5931\u8d25&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001re.findall<\/h4>\n<\/p>\n<p><p><code>re.findall<\/code>\u51fd\u6570\u7528\u4e8e\u641c\u7d22\u5b57\u7b26\u4e32\uff0c\u8fd4\u56de\u6240\u6709\u5339\u914d\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u5b50\u4e32\u7ec4\u6210\u7684\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">pattern = r&quot;\\d+&quot;<\/p>\n<p>text = &quot;123 abc 456 def&quot;<\/p>\n<p>matches = re.findall(pattern, text)<\/p>\n<p>print(matches)  # \u8f93\u51fa\uff1a[&#39;123&#39;, &#39;456&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u9ad8\u7ea7\u6280\u5de7<\/h3>\n<\/p>\n<p><h4>1\u3001\u5206\u7ec4\u548c\u5f15\u7528<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528\u5c0f\u62ec\u53f7<code>()<\/code>\u53ef\u4ee5\u5c06\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u4e00\u90e8\u5206\u8fdb\u884c\u5206\u7ec4\u3002\u5206\u7ec4\u540e\u7684\u5b50\u6a21\u5f0f\u53ef\u4ee5\u901a\u8fc7<code>\\1<\/code>\u3001<code>\\2<\/code>\u7b49\u8fdb\u884c\u5f15\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">pattern = r&quot;(abc)+&quot;<\/p>\n<p>text = &quot;abcabc&quot;<\/p>\n<p>match = re.match(pattern, text)<\/p>\n<p>print(match)  # \u8f93\u51fa\uff1a&lt;re.Match object; span=(0, 6), match=&#39;abcabc&#39;&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u975e\u8d2a\u5a6a\u5339\u914d<\/h4>\n<\/p>\n<p><p>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u8d2a\u5a6a\u7684\uff0c\u5373\u5c3d\u53ef\u80fd\u591a\u5730\u5339\u914d\u5b57\u7b26\u3002\u975e\u8d2a\u5a6a\u5339\u914d\u4f7f\u7528<code>?<\/code>\u8fdb\u884c\u4fee\u9970\uff0c\u5339\u914d\u5c3d\u53ef\u80fd\u5c11\u7684\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">pattern = r&quot;&lt;.*?&gt;&quot;<\/p>\n<p>text = &quot;&lt;div&gt;hello&lt;\/div&gt;&quot;<\/p>\n<p>match = re.search(pattern, text)<\/p>\n<p>print(match.group())  # \u8f93\u51fa\uff1a&lt;div&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u65ad\u8a00<\/h4>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\u652f\u6301\u65ad\u8a00\uff08Assertions\uff09\uff0c\u5305\u62ec\u96f6\u5bbd\u5ea6\u6b63\u9884\u6d4b\u5148\u884c\u65ad\u8a00<code>(?=...)<\/code>\u548c\u96f6\u5bbd\u5ea6\u8d1f\u9884\u6d4b\u5148\u884c\u65ad\u8a00<code>(?!...)<\/code>\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">pattern = r&quot;\\b\\w+(?=\\sis)&quot;<\/p>\n<p>text = &quot;this is a test&quot;<\/p>\n<p>match = re.search(pattern, text)<\/p>\n<p>print(match.group())  # \u8f93\u51fa\uff1athis<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u5b9e\u9645\u5e94\u7528<\/h3>\n<\/p>\n<p><h4>1\u3001\u4ece\u7f51\u9875\u6293\u53d6\u4fe1\u606f<\/h4>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\u5e38\u7528\u4e8e\u4ece\u7f51\u9875\u6293\u53d6\u4fe1\u606f\uff0c\u5982\u63d0\u53d6URL\u3001\u90ae\u7bb1\u5730\u5740\u3001\u7535\u8bdd\u53f7\u7801\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>import requests<\/p>\n<p>url = &quot;http:\/\/example.com&quot;<\/p>\n<p>response = requests.get(url)<\/p>\n<p>html = response.text<\/p>\n<h2><strong>\u63d0\u53d6\u6240\u6709\u94fe\u63a5<\/strong><\/h2>\n<p>pattern = r&#39;href=&quot;(http[s]?:\/\/.*?)&quot;&#39;<\/p>\n<p>links = re.findall(pattern, html)<\/p>\n<p>print(links)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u6570\u636e\u6e05\u6d17<\/h4>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5904\u7406\u4e2d\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f\u53ef\u4ee5\u7528\u4e8e\u6e05\u6d17\u6570\u636e\uff0c\u5982\u53bb\u9664\u591a\u4f59\u7684\u7a7a\u767d\u5b57\u7b26\u3001\u63d0\u53d6\u6709\u7528\u7684\u4fe1\u606f\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">pattern = r&quot;\\s+&quot;<\/p>\n<p>text = &quot;hello   world&quot;<\/p>\n<p>clean_text = re.sub(pattern, &quot; &quot;, text)<\/p>\n<p>print(clean_text)  # \u8f93\u51fa\uff1ahello world<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u65e5\u5fd7\u5206\u6790<\/h4>\n<\/p>\n<p><p>\u5728\u65e5\u5fd7\u5206\u6790\u4e2d\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f\u53ef\u4ee5\u7528\u4e8e\u89e3\u6790\u65e5\u5fd7\u6587\u4ef6\uff0c\u63d0\u53d6\u6709\u7528\u7684\u4fe1\u606f\uff0c\u5982IP\u5730\u5740\u3001\u65f6\u95f4\u6233\u3001\u9519\u8bef\u4fe1\u606f\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">pattern = r&quot;\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}&quot;<\/p>\n<p>log = &quot;2023-10-01 12:00:00 ERROR Something went wrong&quot;<\/p>\n<p>match = re.search(pattern, log)<\/p>\n<p>if match:<\/p>\n<p>    print(match.group())  # \u8f93\u51fa\uff1a2023-10-01 12:00:00<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u5728Python\u4e2d\u901a\u8fc7re\u6a21\u5757\u53ef\u4ee5\u9ad8\u6548\u5730\u8fdb\u884c\u6587\u672c\u5339\u914d\u548c\u5904\u7406\u3002<strong>\u5b9a\u4e49\u6b63\u786e\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f\u3001\u4f7f\u7528re\u6a21\u5757\u7684\u57fa\u672c\u65b9\u6cd5\u3001\u638c\u63e1\u9ad8\u7ea7\u6280\u5de7\u548c\u5b9e\u9645\u5e94\u7528<\/strong>\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5728\u5b9e\u9645\u5de5\u4f5c\u4e2d\u89e3\u51b3\u590d\u6742\u7684\u6587\u672c\u5904\u7406\u95ee\u9898\u3002\u5e0c\u671b\u901a\u8fc7\u672c\u6587\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u80fd\u591f\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u63d0\u9ad8\u5de5\u4f5c\u6548\u7387\u3002<\/p>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\u867d\u7136\u5f3a\u5927\uff0c\u4f46\u4e5f\u6709\u4e00\u5b9a\u7684\u5b66\u4e60\u66f2\u7ebf\u3002\u5efa\u8bae\u5728\u5b9e\u9645\u4f7f\u7528\u4e2d\uff0c\u591a\u52a0\u7ec3\u4e60\u548c\u603b\u7ed3\uff0c\u4e0d\u65ad\u63d0\u9ad8\u81ea\u5df1\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u6280\u80fd\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u8fdb\u884c\u4fe1\u606f\u63d0\u53d6\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>re<\/code>\u6a21\u5757\u6765\u5904\u7406\u6b63\u5219\u8868\u8fbe\u5f0f\u3002\u9996\u5148\uff0c\u5bfc\u5165<code>re<\/code>\u6a21\u5757\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528<code>re.search()<\/code>\u3001<code>re.match()<\/code>\u548c<code>re.findall()<\/code>\u7b49\u51fd\u6570\u6765\u63d0\u53d6\u4fe1\u606f\u3002\u901a\u8fc7\u5b9a\u4e49\u4e00\u4e2a\u5408\u9002\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f\uff0c\u7ed3\u5408\u8fd9\u4e9b\u51fd\u6570\uff0c\u53ef\u4ee5\u4ece\u5b57\u7b26\u4e32\u4e2d\u6293\u53d6\u6240\u9700\u7684\u6570\u636e\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>re.findall(r&#39;\\d+&#39;, text)<\/code>\u53ef\u4ee5\u6293\u53d6\u6587\u672c\u4e2d\u7684\u6240\u6709\u6570\u5b57\u3002<\/p>\n<p><strong>\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u57fa\u672c\u8bed\u6cd5\u6709\u54ea\u4e9b\uff1f<\/strong><br \/>\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u57fa\u672c\u8bed\u6cd5\u5305\u62ec\u5b57\u7b26\u7c7b\u3001\u951a\u70b9\u3001\u91cf\u8bcd\u7b49\u3002\u5b57\u7b26\u7c7b\u7528\u65b9\u62ec\u53f7<code>[]<\/code>\u8868\u793a\uff0c\u53ef\u4ee5\u5339\u914d\u5176\u4e2d\u7684\u4efb\u610f\u5b57\u7b26\uff1b\u951a\u70b9\u5982<code>^<\/code>\u548c<code>$<\/code>\u5206\u522b\u8868\u793a\u5b57\u7b26\u4e32\u7684\u5f00\u5934\u548c\u7ed3\u5c3e\uff1b\u91cf\u8bcd\u5982<code>*<\/code>\u3001<code>+<\/code>\u548c<code>?<\/code>\u7528\u6765\u6307\u5b9a\u5b57\u7b26\u51fa\u73b0\u7684\u6b21\u6570\u3002\u4f8b\u5982\uff0c<code>[a-z]+<\/code>\u53ef\u4ee5\u5339\u914d\u4e00\u4e2a\u6216\u591a\u4e2a\u5c0f\u5199\u5b57\u6bcd\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u8c03\u8bd5\u6b63\u5219\u8868\u8fbe\u5f0f\uff1f<\/strong><br \/>\u8c03\u8bd5\u6b63\u5219\u8868\u8fbe\u5f0f\u53ef\u4ee5\u4f7f\u7528\u5728\u7ebf\u5de5\u5177\uff0c\u5982Regex101\u6216Regexr\uff0c\u8fd9\u4e9b\u5de5\u5177\u5141\u8bb8\u4f60\u5b9e\u65f6\u6d4b\u8bd5\u6b63\u5219\u8868\u8fbe\u5f0f\u5e76\u67e5\u770b\u5339\u914d\u7ed3\u679c\u3002\u5728Python\u4e2d\uff0c\u4e5f\u53ef\u4ee5\u5728\u4ee3\u7801\u4e2d\u6dfb\u52a0\u6253\u5370\u8bed\u53e5\uff0c\u67e5\u770b\u4e2d\u95f4\u53d8\u91cf\u7684\u503c\uff0c\u6216\u8005\u4f7f\u7528<code>re.debug<\/code>\u65b9\u6cd5\u6765\u5206\u6790\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u6267\u884c\u8fc7\u7a0b\uff0c\u4ee5\u786e\u4fdd\u5176\u7b26\u5408\u9884\u671f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6293\u53d6\u4fe1\u606f\u7684\u6838\u5fc3\u8981\u70b9\u6709\uff1a\u4f7f\u7528re\u6a21\u5757\u3001\u5b9a\u4e49\u6b63\u786e\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f\u3001\u4f7f\u7528re.search [&hellip;]","protected":false},"author":3,"featured_media":1140233,"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\/1140225"}],"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=1140225"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1140225\/revisions"}],"predecessor-version":[{"id":1140235,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1140225\/revisions\/1140235"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1140233"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1140225"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1140225"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1140225"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}