{"id":985687,"date":"2024-12-27T07:38:44","date_gmt":"2024-12-26T23:38:44","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/985687.html"},"modified":"2024-12-27T07:38:48","modified_gmt":"2024-12-26T23:38:48","slug":"python%e5%a6%82%e4%bd%95%e5%af%bc%e5%85%a5re%e5%8c%85","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/985687.html","title":{"rendered":"python\u5982\u4f55\u5bfc\u5165re\u5305"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24212730\/fcf1a18b-a308-4bd8-acb7-d9e988f105f6.webp\" alt=\"python\u5982\u4f55\u5bfc\u5165re\u5305\" \/><\/p>\n<p><p> \u8981\u5bfc\u5165Python\u7684<code>re<\/code>\u5305\uff0c\u53ea\u9700\u5728Python\u811a\u672c\u4e2d\u4f7f\u7528<code>import re<\/code>\u8bed\u53e5\u5373\u53ef\u3002<strong>\u5728Python\u4e2d\uff0c<code>re<\/code>\u6a21\u5757\u7528\u4e8e\u6b63\u5219\u8868\u8fbe\u5f0f\u64cd\u4f5c\uff0c\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u51fd\u6570\u548c\u65b9\u6cd5\u7528\u4e8e\u5b57\u7b26\u4e32\u641c\u7d22\u548c\u66ff\u6362\u3001\u6a21\u5f0f\u5339\u914d\u7b49\u64cd\u4f5c<\/strong>\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5173\u952e\u70b9\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u7b80\u5355\u5bfc\u5165<\/strong>\uff1a\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528<code>import re<\/code>\u6765\u5bfc\u5165\u6a21\u5757\u3002<\/li>\n<li><strong>\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d<\/strong>\uff1a<code>re<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86<code>match<\/code>\u3001<code>search<\/code>\u3001<code>findall<\/code>\u7b49\u65b9\u6cd5\u6765\u8fdb\u884c\u4e0d\u540c\u5f62\u5f0f\u7684\u5339\u914d\u3002<\/li>\n<li><strong>\u7075\u6d3b\u6027\u548c\u529f\u80fd\u6027<\/strong>\uff1a\u5229\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u53ef\u4ee5\u5b9e\u73b0\u590d\u6742\u7684\u5b57\u7b26\u4e32\u5904\u7406\u4efb\u52a1\uff0c\u5982\u6570\u636e\u9a8c\u8bc1\u3001\u6587\u672c\u89e3\u6790\u7b49\u3002<\/li>\n<\/ul>\n<p><p>\u5177\u4f53\u6765\u8bf4\uff0c<code>re<\/code>\u6a21\u5757\u4e2d\u7684<code>match<\/code>\u548c<code>search<\/code>\u65b9\u6cd5\u662f\u7528\u4e8e\u5339\u914d\u5b57\u7b26\u4e32\u4e2d\u7684\u6a21\u5f0f\u3002<code>match<\/code>\u4ece\u5b57\u7b26\u4e32\u7684\u8d77\u59cb\u4f4d\u7f6e\u5f00\u59cb\u5339\u914d\uff0c\u800c<code>search<\/code>\u5728\u6574\u4e2a\u5b57\u7b26\u4e32\u4e2d\u641c\u7d22\u5339\u914d\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>pattern = r&quot;hello&quot;<\/p>\n<p>text = &quot;hello world&quot;<\/p>\n<h2><strong>\u4f7f\u7528match\u65b9\u6cd5<\/strong><\/h2>\n<p>match_result = re.match(pattern, text)<\/p>\n<p>if match_result:<\/p>\n<p>    print(&quot;Match found:&quot;, match_result.group())<\/p>\n<p>else:<\/p>\n<p>    print(&quot;No match found&quot;)<\/p>\n<h2><strong>\u4f7f\u7528search\u65b9\u6cd5<\/strong><\/h2>\n<p>search_result = re.search(pattern, text)<\/p>\n<p>if search_result:<\/p>\n<p>    print(&quot;Search found:&quot;, search_result.group())<\/p>\n<p>else:<\/p>\n<p>    print(&quot;No search found&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u6df1\u5165\u63a2\u8ba8Python <code>re<\/code>\u6a21\u5757\u7684\u5404\u79cd\u529f\u80fd\u548c\u4f7f\u7528\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<h2><strong>\u4e00\u3001RE\u6a21\u5757\u57fa\u7840<\/strong><\/h2>\n<p><p><code>re<\/code>\u6a21\u5757\u662fPython\u6807\u51c6\u5e93\u4e2d\u7684\u4e00\u4e2a\u6a21\u5757\uff0c\u4e13\u95e8\u7528\u4e8e\u5904\u7406\u6b63\u5219\u8868\u8fbe\u5f0f\u3002\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u7528\u4e8e\u5339\u914d\u5b57\u7b26\u4e32\u6a21\u5f0f\u3002\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u8bed\u6cd5\u5728\u4e0d\u540c\u7684\u7f16\u7a0b\u8bed\u8a00\u4e2d\u5927\u540c\u5c0f\u5f02\uff0c\u4f46\u5177\u4f53\u5b9e\u73b0\u53ef\u80fd\u4f1a\u6709\u6240\u5dee\u5f02\u3002Python\u7684<code>re<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u5957\u529f\u80fd\u4e30\u5bcc\u7684API\u6765\u5904\u7406\u6b63\u5219\u8868\u8fbe\u5f0f\u3002<\/p>\n<\/p>\n<p><h2>1.1\u3001\u5bfc\u5165\u548c\u57fa\u672c\u7528\u6cd5<\/h2>\n<\/p>\n<p><p>\u8981\u4f7f\u7528<code>re<\/code>\u6a21\u5757\uff0c\u53ea\u9700\u5728\u4f60\u7684Python\u811a\u672c\u4e2d\u5bfc\u5165\u5b83\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e00\u65e6\u5bfc\u5165\uff0c\u4f60\u5c31\u53ef\u4ee5\u4f7f\u7528<code>re<\/code>\u6a21\u5757\u4e2d\u63d0\u4f9b\u7684\u5404\u79cd\u51fd\u6570\u6765\u5904\u7406\u6b63\u5219\u8868\u8fbe\u5f0f\u3002\u5e38\u7528\u7684\u51fd\u6570\u5305\u62ec\uff1a<\/p>\n<\/p>\n<ul>\n<li><code>re.match(pattern, string, flags=0)<\/code>\uff1a\u4ece\u5b57\u7b26\u4e32\u7684\u8d77\u59cb\u4f4d\u7f6e\u8fdb\u884c\u5339\u914d\u3002<\/li>\n<li><code>re.search(pattern, string, flags=0)<\/code>\uff1a\u641c\u7d22\u6574\u4e2a\u5b57\u7b26\u4e32\uff0c\u8fd4\u56de\u7b2c\u4e00\u4e2a\u5339\u914d\u7684\u7ed3\u679c\u3002<\/li>\n<li><code>re.findall(pattern, string, flags=0)<\/code>\uff1a\u8fd4\u56de\u5b57\u7b26\u4e32\u4e2d\u6240\u6709\u4e0e\u6a21\u5f0f\u5339\u914d\u7684\u5b50\u4e32\u3002<\/li>\n<\/ul>\n<p><h2>1.2\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u8bed\u6cd5<\/h2>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\u7531\u666e\u901a\u5b57\u7b26\uff08\u4f8b\u5982\uff0c\u5b57\u6bcd\u3001\u6570\u5b57\uff09\u548c\u7279\u6b8a\u5b57\u7b26\uff08\u4e5f\u79f0\u4e3a\u5143\u5b57\u7b26\uff09\u7ec4\u6210\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u5143\u5b57\u7b26\uff1a<\/p>\n<\/p>\n<ul>\n<li><code>.<\/code>\uff1a\u5339\u914d\u4efb\u4f55\u5355\u4e2a\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}<\/code>\uff1a\u7cbe\u786e\u5339\u914d\u524d\u9762\u7684\u5b57\u7b26n\u6b21\u3002<\/li>\n<\/ul>\n<h2><strong>\u4e8c\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u51fd\u6570\u8be6\u89e3<\/strong><\/h2>\n<p><p>Python\u7684<code>re<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u591a\u79cd\u51fd\u6570\uff0c\u53ef\u4ee5\u6839\u636e\u4e0d\u540c\u7684\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u51fd\u6570\u6765\u64cd\u4f5c\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><h2>2.1\u3001re.match()\u51fd\u6570<\/h2>\n<\/p>\n<p><p><code>re.match()<\/code>\u51fd\u6570\u5c1d\u8bd5\u4ece\u5b57\u7b26\u4e32\u7684\u8d77\u59cb\u4f4d\u7f6e\u5339\u914d\u4e00\u4e2a\u6a21\u5f0f\u3002\u5982\u679c\u5339\u914d\u6210\u529f\uff0c\u8fd4\u56de\u4e00\u4e2a\u5339\u914d\u5bf9\u8c61\uff0c\u5426\u5219\u8fd4\u56de<code>None<\/code>\u3002\u5339\u914d\u5bf9\u8c61\u5305\u542b\u5339\u914d\u7684\u8be6\u7ec6\u4fe1\u606f\uff0c\u53ef\u4ee5\u901a\u8fc7<code>group()<\/code>\u65b9\u6cd5\u83b7\u53d6\u5339\u914d\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>pattern = r&quot;hello&quot;<\/p>\n<p>text = &quot;hello world&quot;<\/p>\n<p>match_result = re.match(pattern, text)<\/p>\n<p>if match_result:<\/p>\n<p>    print(&quot;Match found:&quot;, match_result.group())<\/p>\n<p>else:<\/p>\n<p>    print(&quot;No match found&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>2.2\u3001re.search()\u51fd\u6570<\/h2>\n<\/p>\n<p><p><code>re.search()<\/code>\u51fd\u6570\u626b\u63cf\u6574\u4e2a\u5b57\u7b26\u4e32\u5e76\u8fd4\u56de\u7b2c\u4e00\u4e2a\u5339\u914d\u7684\u5bf9\u8c61\u3002\u5982\u679c\u6ca1\u6709\u627e\u5230\u5339\u914d\uff0c\u5219\u8fd4\u56de<code>None<\/code>\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>pattern = r&quot;world&quot;<\/p>\n<p>text = &quot;hello world&quot;<\/p>\n<p>search_result = re.search(pattern, text)<\/p>\n<p>if search_result:<\/p>\n<p>    print(&quot;Search found:&quot;, search_result.group())<\/p>\n<p>else:<\/p>\n<p>    print(&quot;No search found&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>2.3\u3001re.findall()\u51fd\u6570<\/h2>\n<\/p>\n<p><p><code>re.findall()<\/code>\u51fd\u6570\u8fd4\u56de\u5b57\u7b26\u4e32\u4e2d\u6240\u6709\u4e0e\u6a21\u5f0f\u5339\u914d\u7684\u5b50\u4e32\uff0c\u4ee5\u5217\u8868\u7684\u5f62\u5f0f\u8fd4\u56de\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>pattern = r&quot;\\d+&quot;<\/p>\n<p>text = &quot;There are 123 apples and 456 oranges.&quot;<\/p>\n<p>findall_result = re.findall(pattern, text)<\/p>\n<p>print(&quot;Findall result:&quot;, findall_result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u4e09\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u9ad8\u7ea7\u7528\u6cd5<\/strong><\/h2>\n<p><p>\u9664\u4e86\u57fa\u672c\u7684\u5339\u914d\u529f\u80fd\uff0c<code>re<\/code>\u6a21\u5757\u8fd8\u652f\u6301\u8bb8\u591a\u9ad8\u7ea7\u529f\u80fd\uff0c\u4f8b\u5982\u5206\u7ec4\u3001\u66ff\u6362\u548c\u7f16\u8bd1\u6b63\u5219\u8868\u8fbe\u5f0f\u3002<\/p>\n<\/p>\n<p><h2>3.1\u3001\u5206\u7ec4\u548c\u6355\u83b7<\/h2>\n<\/p>\n<p><p>\u5728\u6b63\u5219\u8868\u8fbe\u5f0f\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5706\u62ec\u53f7<code>()<\/code>\u6765\u521b\u5efa\u5206\u7ec4\u3002\u8fd9\u4e9b\u5206\u7ec4\u53ef\u4ee5\u5728\u5339\u914d\u65f6\u88ab\u6355\u83b7\uff0c\u5e76\u901a\u8fc7\u5339\u914d\u5bf9\u8c61\u7684<code>group()<\/code>\u65b9\u6cd5\u8bbf\u95ee\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>pattern = r&quot;(\\d+)-(\\d+)&quot;<\/p>\n<p>text = &quot;123-456&quot;<\/p>\n<p>match_result = re.match(pattern, text)<\/p>\n<p>if match_result:<\/p>\n<p>    print(&quot;Full match:&quot;, match_result.group(0))<\/p>\n<p>    print(&quot;Group 1:&quot;, match_result.group(1))<\/p>\n<p>    print(&quot;Group 2:&quot;, match_result.group(2))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>3.2\u3001\u66ff\u6362<\/h2>\n<\/p>\n<p><p><code>re.sub()<\/code>\u51fd\u6570\u7528\u4e8e\u66ff\u6362\u5b57\u7b26\u4e32\u4e2d\u7684\u5339\u914d\u9879\u3002\u5b83\u63a5\u53d7\u4e00\u4e2a\u66ff\u6362\u5b57\u7b26\u4e32\u6216\u4e00\u4e2a\u66ff\u6362\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>pattern = r&quot;\\d+&quot;<\/p>\n<p>text = &quot;There are 123 apples and 456 oranges.&quot;<\/p>\n<p>sub_result = re.sub(pattern, &quot;number&quot;, text)<\/p>\n<p>print(&quot;Sub result:&quot;, sub_result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>3.3\u3001\u7f16\u8bd1\u6b63\u5219\u8868\u8fbe\u5f0f<\/h2>\n<\/p>\n<p><p>\u5bf9\u4e8e\u9891\u7e41\u4f7f\u7528\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u53ef\u4ee5\u901a\u8fc7<code>re.compile()<\/code>\u51fd\u6570\u8fdb\u884c\u7f16\u8bd1\u3002\u7f16\u8bd1\u540e\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u53ef\u4ee5\u63d0\u9ad8\u5339\u914d\u901f\u5ea6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>pattern = re.compile(r&quot;\\d+&quot;)<\/p>\n<p>text = &quot;There are 123 apples and 456 oranges.&quot;<\/p>\n<p>findall_result = pattern.findall(text)<\/p>\n<p>print(&quot;Compiled findall result:&quot;, findall_result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u56db\u3001RE\u6a21\u5757\u7684\u7279\u6b8a\u529f\u80fd<\/strong><\/h2>\n<p><p>Python\u7684<code>re<\/code>\u6a21\u5757\u8fd8\u652f\u6301\u4e00\u4e9b\u7279\u6b8a\u529f\u80fd\uff0c\u4f8b\u5982\u5904\u7406\u591a\u884c\u5b57\u7b26\u4e32\u3001\u5ffd\u7565\u5927\u5c0f\u5199\u7b49\u3002<\/p>\n<\/p>\n<p><h2>4.1\u3001\u591a\u884c\u5339\u914d<\/h2>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>re.MULTILINE<\/code>\u6807\u5fd7\u6765\u5904\u7406\u591a\u884c\u5b57\u7b26\u4e32\u3002\u5728\u591a\u884c\u6a21\u5f0f\u4e0b\uff0c<code>^<\/code>\u548c<code>$<\/code>\u5206\u522b\u5339\u914d\u6bcf\u4e00\u884c\u7684\u5f00\u59cb\u548c\u7ed3\u675f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>pattern = r&quot;^hello&quot;<\/p>\n<p>text = &quot;&quot;&quot;hello world<\/p>\n<p>goodbye world<\/p>\n<p>hello ag<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n&quot;&quot;&quot;<\/p>\n<p>multiline_result = re.findall(pattern, text, re.MULTILINE)<\/p>\n<p>print(&quot;Multiline result:&quot;, multiline_result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>4.2\u3001\u5ffd\u7565\u5927\u5c0f\u5199<\/h2>\n<\/p>\n<p><p>\u4f7f\u7528<code>re.IGNORECASE<\/code>\u6807\u5fd7\u53ef\u4ee5\u5ffd\u7565\u6b63\u5219\u8868\u8fbe\u5f0f\u4e2d\u7684\u5927\u5c0f\u5199\u5dee\u5f02\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>pattern = r&quot;hello&quot;<\/p>\n<p>text = &quot;Hello world&quot;<\/p>\n<p>ignorecase_result = re.search(pattern, text, re.IGNORECASE)<\/p>\n<p>if ignorecase_result:<\/p>\n<p>    print(&quot;Ignorecase search found:&quot;, ignorecase_result.group())<\/p>\n<p>else:<\/p>\n<p>    print(&quot;No ignorecase search found&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u4e94\u3001\u5e38\u89c1\u6b63\u5219\u8868\u8fbe\u5f0f\u5e94\u7528<\/strong><\/h2>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\u5728\u6570\u636e\u9a8c\u8bc1\u3001\u6587\u672c\u89e3\u6790\u548c\u4fe1\u606f\u63d0\u53d6\u7b49\u65b9\u9762\u6709\u5e7f\u6cdb\u7684\u5e94\u7528\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u5e94\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h2>5.1\u3001\u7535\u5b50\u90ae\u4ef6\u9a8c\u8bc1<\/h2>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u9a8c\u8bc1\u7535\u5b50\u90ae\u4ef6\u5730\u5740\u7684\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>pattern = r&quot;^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$&quot;<\/p>\n<p>email = &quot;example@example.com&quot;<\/p>\n<p>email_result = re.match(pattern, email)<\/p>\n<p>if email_result:<\/p>\n<p>    print(&quot;Valid email address&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;Invalid email address&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>5.2\u3001\u7535\u8bdd\u53f7\u7801\u63d0\u53d6<\/h2>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\u53ef\u4ee5\u7528\u6765\u4ece\u6587\u672c\u4e2d\u63d0\u53d6\u7535\u8bdd\u53f7\u7801\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>pattern = r&quot;\\b\\d{3}[-.]?\\d{3}[-.]?\\d{4}\\b&quot;<\/p>\n<p>text = &quot;Call me at 123-456-7890 or 987.654.3210.&quot;<\/p>\n<p>phone_numbers = re.findall(pattern, text)<\/p>\n<p>print(&quot;Phone numbers found:&quot;, phone_numbers)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>5.3\u3001URL\u63d0\u53d6<\/h2>\n<\/p>\n<p><p>\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u53ef\u4ee5\u4ece\u6587\u672c\u4e2d\u63d0\u53d6URL\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>pattern = r&quot;https?:\/\/(?:[-\\w.]|(?:%[\\da-fA-F]{2}))+&quot;<\/p>\n<p>text = &quot;Visit https:\/\/www.example.com or http:\/\/example.org.&quot;<\/p>\n<p>urls = re.findall(pattern, text)<\/p>\n<p>print(&quot;URLs found:&quot;, urls)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u5bf9Python <code>re<\/code>\u6a21\u5757\u7684\u5b66\u4e60\u548c\u5b9e\u8df5\uff0c\u7528\u6237\u53ef\u4ee5\u638c\u63e1\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u57fa\u672c\u7528\u6cd5\u53ca\u5176\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u7684\u5f3a\u5927\u529f\u80fd\u3002\u65e0\u8bba\u662f\u7b80\u5355\u7684\u5b57\u7b26\u4e32\u5339\u914d\uff0c\u8fd8\u662f\u590d\u6742\u7684\u6587\u672c\u89e3\u6790\uff0c<code>re<\/code>\u6a21\u5757\u90fd\u63d0\u4f9b\u4e86\u7075\u6d3b\u4e14\u9ad8\u6548\u7684\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b89\u88c5re\u6a21\u5757\uff1f<\/strong><br \/>re\u6a21\u5757\u662fPython\u7684\u6807\u51c6\u5e93\u4e4b\u4e00\uff0c\u901a\u5e38\u4e0d\u9700\u8981\u5355\u72ec\u5b89\u88c5\u3002\u53ea\u9700\u786e\u4fdd\u4f60\u7684Python\u73af\u5883\u5df2\u6b63\u786e\u914d\u7f6e\u5373\u53ef\u4f7f\u7528\u3002\u5728\u4f60\u7684Python\u4ee3\u7801\u4e2d\uff0c\u76f4\u63a5\u4f7f\u7528<code>import re<\/code>\u5c31\u53ef\u4ee5\u8bbf\u95ee\u8be5\u6a21\u5757\u7684\u529f\u80fd\u3002<\/p>\n<p><strong>re\u6a21\u5757\u7684\u4e3b\u8981\u529f\u80fd\u6709\u54ea\u4e9b\uff1f<\/strong><br \/>re\u6a21\u5757\u63d0\u4f9b\u4e86\u591a\u79cd\u5f3a\u5927\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u529f\u80fd\uff0c\u5305\u62ec\u5b57\u7b26\u4e32\u5339\u914d\u3001\u67e5\u627e\u3001\u66ff\u6362\u548c\u5206\u5272\u7b49\u3002\u7528\u6237\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u8fdb\u884c\u590d\u6742\u7684\u5b57\u7b26\u4e32\u5904\u7406\uff0c\u6bd4\u5982\u9a8c\u8bc1\u7535\u5b50\u90ae\u4ef6\u683c\u5f0f\u3001\u63d0\u53d6\u7279\u5b9a\u4fe1\u606f\u7b49\u3002<\/p>\n<p><strong>\u5728\u4f7f\u7528re\u6a21\u5757\u65f6\uff0c\u5982\u4f55\u5904\u7406\u5e38\u89c1\u9519\u8bef\uff1f<\/strong><br \/>\u5e38\u89c1\u7684\u9519\u8bef\u5305\u62ec\u6b63\u5219\u8868\u8fbe\u5f0f\u8bed\u6cd5\u9519\u8bef\u548c\u5339\u914d\u4e0d\u5230\u7684\u60c5\u51b5\u3002\u786e\u4fdd\u4f60\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u9075\u5faa\u6b63\u786e\u7684\u8bed\u6cd5\uff0c\u5e76\u4f7f\u7528<code>try-except<\/code>\u8bed\u53e5\u6765\u6355\u83b7\u53ef\u80fd\u51fa\u73b0\u7684\u5f02\u5e38\u3002\u6b64\u5916\uff0c\u4f7f\u7528<code>re.compile()<\/code>\u63d0\u524d\u7f16\u8bd1\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u53ef\u4ee5\u63d0\u9ad8\u5339\u914d\u6548\u7387\uff0c\u5e76\u5e2e\u52a9\u67e5\u627e\u6f5c\u5728\u7684\u9519\u8bef\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u5bfc\u5165Python\u7684re\u5305\uff0c\u53ea\u9700\u5728Python\u811a\u672c\u4e2d\u4f7f\u7528import re\u8bed\u53e5\u5373\u53ef\u3002\u5728Python\u4e2d\uff0cre\u6a21\u5757 [&hellip;]","protected":false},"author":3,"featured_media":985699,"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\/985687"}],"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=985687"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/985687\/revisions"}],"predecessor-version":[{"id":985701,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/985687\/revisions\/985701"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/985699"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=985687"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=985687"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=985687"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}