{"id":1080709,"date":"2025-01-08T12:32:46","date_gmt":"2025-01-08T04:32:46","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1080709.html"},"modified":"2025-01-08T12:32:49","modified_gmt":"2025-01-08T04:32:49","slug":"python%e6%ad%a3%e5%88%99%e8%a1%a8%e8%be%be%e5%bc%8f%e5%a6%82%e4%bd%95%e6%89%be-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1080709.html","title":{"rendered":"python\u6b63\u5219\u8868\u8fbe\u5f0f\u5982\u4f55\u627e"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24183115\/3531e2f1-b544-4215-8468-d0d88448e175.webp\" alt=\"python\u6b63\u5219\u8868\u8fbe\u5f0f\u5982\u4f55\u627e\" \/><\/p>\n<p><p> <strong>Python \u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u4f7f\u7528\u975e\u5e38\u65b9\u4fbf\u3001\u529f\u80fd\u5f3a\u5927\u3001\u80fd\u591f\u5904\u7406\u590d\u6742\u7684\u5b57\u7b26\u4e32\u5339\u914d\u9700\u6c42\u3002<\/strong><\/p>\n<\/p>\n<p><p><strong>\u9996\u5148\uff0c\u8981\u4f7f\u7528 Python \u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u60a8\u9700\u8981\u5bfc\u5165\u5185\u7f6e\u7684 <code>re<\/code> \u6a21\u5757\u3002\u5176\u6b21\uff0c\u7406\u89e3\u548c\u7f16\u5199\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f\u662f\u5173\u952e\u3002\u6b63\u5219\u8868\u8fbe\u5f0f\u53ef\u4ee5\u7528\u6765\u67e5\u627e\u3001\u5339\u914d\u3001\u66ff\u6362\u548c\u62c6\u5206\u5b57\u7b26\u4e32\u4e2d\u7684\u7279\u5b9a\u6a21\u5f0f\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5728 Python \u4e2d\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u627e\u5230\u7279\u5b9a\u7684\u5185\u5bb9\uff1a<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u5bfc\u5165 <code>re<\/code> \u6a21\u5757<\/p>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u4e4b\u524d\uff0c\u9700\u8981\u5bfc\u5165 Python \u5185\u7f6e\u7684 <code>re<\/code> \u6a21\u5757\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u7f16\u5199\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f<\/p>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f\u662f\u4e00\u79cd\u7528\u6765\u63cf\u8ff0\u5b57\u7b26\u7ec4\u5408\u7684\u5b57\u7b26\u4e32\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u7684\u6a21\u5f0f\uff1a<\/p>\n<\/p>\n<ul>\n<li><code>.<\/code> \u5339\u914d\u4efb\u610f\u5b57\u7b26\uff08\u9664\u6362\u884c\u7b26\uff09\u3002<\/li>\n<li><code>\\d<\/code> \u5339\u914d\u4e00\u4e2a\u6570\u5b57\u5b57\u7b26\uff0c\u76f8\u5f53\u4e8e <code>[0-9]<\/code>\u3002<\/li>\n<li><code>\\D<\/code> \u5339\u914d\u4e00\u4e2a\u975e\u6570\u5b57\u5b57\u7b26\uff0c\u76f8\u5f53\u4e8e <code>[^0-9]<\/code>\u3002<\/li>\n<li><code>\\w<\/code> \u5339\u914d\u4e00\u4e2a\u5b57\u6bcd\u6570\u5b57\u5b57\u7b26\uff0c\u76f8\u5f53\u4e8e <code>[a-zA-Z0-9_]<\/code>\u3002<\/li>\n<li><code>\\W<\/code> \u5339\u914d\u4e00\u4e2a\u975e\u5b57\u6bcd\u6570\u5b57\u5b57\u7b26\uff0c\u76f8\u5f53\u4e8e <code>[^a-zA-Z0-9_]<\/code>\u3002<\/li>\n<li><code>\\s<\/code> \u5339\u914d\u4e00\u4e2a\u7a7a\u767d\u5b57\u7b26\uff0c\u76f8\u5f53\u4e8e <code>[ \\t\\n\\r\\f\\v]<\/code>\u3002<\/li>\n<li><code>\\S<\/code> \u5339\u914d\u4e00\u4e2a\u975e\u7a7a\u767d\u5b57\u7b26\uff0c\u76f8\u5f53\u4e8e <code>[^ \\t\\n\\r\\f\\v]<\/code>\u3002<\/li>\n<\/ul>\n<p><p>\u4e09\u3001\u57fa\u672c\u65b9\u6cd5<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong><code>re.match()<\/code><\/strong><\/p>\n<\/p>\n<p><p><code>re.match()<\/code> \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\u5219\u8fd4\u56de\u4e00\u4e2a\u5339\u914d\u5bf9\u8c61\uff1b\u5426\u5219\u8fd4\u56de <code>None<\/code>\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">pattern = r&#39;\\d+&#39;<\/p>\n<p>string = &#39;123abc&#39;<\/p>\n<p>match = re.match(pattern, string)<\/p>\n<p>if match:<\/p>\n<p>    print(&#39;Match found:&#39;, match.group())<\/p>\n<p>else:<\/p>\n<p>    print(&#39;No match&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong><code>re.search()<\/code><\/strong><\/p>\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\">pattern = r&#39;\\d+&#39;<\/p>\n<p>string = &#39;abc123def&#39;<\/p>\n<p>match = re.search(pattern, string)<\/p>\n<p>if match:<\/p>\n<p>    print(&#39;Search found:&#39;, match.group())<\/p>\n<p>else:<\/p>\n<p>    print(&#39;No match&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong><code>re.findall()<\/code><\/strong><\/p>\n<\/p>\n<p><p><code>re.findall()<\/code> \u8fd4\u56de\u5b57\u7b26\u4e32\u4e2d\u6240\u6709\u4e0d\u91cd\u53e0\u7684\u5339\u914d\u5bf9\u8c61\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">pattern = r&#39;\\d+&#39;<\/p>\n<p>string = &#39;abc123def456&#39;<\/p>\n<p>matches = re.findall(pattern, string)<\/p>\n<p>print(&#39;Findall found:&#39;, matches)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong><code>re.finditer()<\/code><\/strong><\/p>\n<\/p>\n<p><p><code>re.finditer()<\/code> \u8fd4\u56de\u4e00\u4e2a\u5339\u914d\u5bf9\u8c61\u7684\u8fed\u4ee3\u5668\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">pattern = r&#39;\\d+&#39;<\/p>\n<p>string = &#39;abc123def456&#39;<\/p>\n<p>matches = re.finditer(pattern, string)<\/p>\n<p>for match in matches:<\/p>\n<p>    print(&#39;Finditer found:&#39;, match.group())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u56db\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u9ad8\u7ea7\u7528\u6cd5<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u6355\u83b7\u7ec4<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u62ec\u53f7 <code>()<\/code> \u53ef\u4ee5\u5728\u6b63\u5219\u8868\u8fbe\u5f0f\u4e2d\u521b\u5efa\u6355\u83b7\u7ec4\uff0c\u4ee5\u4fbf\u5728\u5339\u914d\u65f6\u63d0\u53d6\u5b50\u6a21\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">pattern = r&#39;(\\d+)-(\\d+)-(\\d+)&#39;<\/p>\n<p>string = &#39;123-456-789&#39;<\/p>\n<p>match = re.match(pattern, string)<\/p>\n<p>if match:<\/p>\n<p>    print(&#39;Full match:&#39;, match.group())<\/p>\n<p>    print(&#39;Group 1:&#39;, match.group(1))<\/p>\n<p>    print(&#39;Group 2:&#39;, match.group(2))<\/p>\n<p>    print(&#39;Group 3:&#39;, match.group(3))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u975e\u6355\u83b7\u7ec4<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528 <code>(?:...)<\/code> \u53ef\u4ee5\u521b\u5efa\u975e\u6355\u83b7\u7ec4\uff0c\u8fd9\u5bf9\u4e8e\u4ec5\u9700\u8981\u5339\u914d\u800c\u4e0d\u9700\u8981\u63d0\u53d6\u7684\u60c5\u51b5\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">pattern = r&#39;(?:\\d+)-(\\d+)-(\\d+)&#39;<\/p>\n<p>string = &#39;123-456-789&#39;<\/p>\n<p>match = re.match(pattern, string)<\/p>\n<p>if match:<\/p>\n<p>    print(&#39;Full match:&#39;, match.group())<\/p>\n<p>    print(&#39;Group 1:&#39;, match.group(1))<\/p>\n<p>    print(&#39;Group 2:&#39;, match.group(2))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u96f6\u5bbd\u65ad\u8a00<\/strong><\/p>\n<\/p>\n<p><p>\u96f6\u5bbd\u65ad\u8a00\u5305\u62ec\u6b63\u5411\u5148\u884c\u65ad\u8a00 <code>(?=...)<\/code> \u548c\u6b63\u5411\u540e\u53d1\u65ad\u8a00 <code>(?&lt;=...)<\/code>\uff0c\u7528\u4e8e\u5339\u914d\u67d0\u4e2a\u4f4d\u7f6e\u524d\u6216\u540e\u7684\u7279\u5b9a\u6a21\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6b63\u5411\u5148\u884c\u65ad\u8a00<\/p>\n<p>pattern = r&#39;\\d+(?=abc)&#39;<\/p>\n<p>string = &#39;123abc456&#39;<\/p>\n<p>match = re.search(pattern, string)<\/p>\n<p>if match:<\/p>\n<p>    print(&#39;Positive lookahead found:&#39;, match.group())<\/p>\n<h2><strong>\u6b63\u5411\u540e\u53d1\u65ad\u8a00<\/strong><\/h2>\n<p>pattern = r&#39;(?&lt;=abc)\\d+&#39;<\/p>\n<p>string = &#39;abc123def456&#39;<\/p>\n<p>match = re.search(pattern, string)<\/p>\n<p>if match:<\/p>\n<p>    print(&#39;Positive lookbehind found:&#39;, match.group())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u66ff\u6362<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528 <code>re.sub()<\/code> \u53ef\u4ee5\u66ff\u6362\u5b57\u7b26\u4e32\u4e2d\u6240\u6709\u7b26\u5408\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u5b50\u4e32\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">pattern = r&#39;\\d+&#39;<\/p>\n<p>string = &#39;abc123def456&#39;<\/p>\n<p>replaced_string = re.sub(pattern, &#39;NUMBER&#39;, string)<\/p>\n<p>print(&#39;Replaced string:&#39;, replaced_string)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u62c6\u5206<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528 <code>re.split()<\/code> \u53ef\u4ee5\u6839\u636e\u6b63\u5219\u8868\u8fbe\u5f0f\u5206\u5272\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">pattern = r&#39;\\d+&#39;<\/p>\n<p>string = &#39;abc123def456&#39;<\/p>\n<p>split_list = re.split(pattern, string)<\/p>\n<p>print(&#39;Split list:&#39;, split_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e94\u3001\u5e38\u89c1\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u5e94\u7528\u573a\u666f<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5339\u914d\u7535\u5b50\u90ae\u4ef6\u5730\u5740<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">pattern = r&#39;[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\\.[a-zA-Z0-9-.]+&#39;<\/p>\n<p>string = &#39;Please contact us at support@example.com for further information.&#39;<\/p>\n<p>match = re.search(pattern, string)<\/p>\n<p>if match:<\/p>\n<p>    print(&#39;Em<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>l found:&#39;, match.group())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5339\u914d\u7535\u8bdd\u53f7\u7801<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">pattern = r&#39;\\(?\\d{3}\\)?-?\\s*\\d{3}-?\\s*\\d{4}&#39;<\/p>\n<p>string = &#39;You can reach me at (123) 456-7890 or 987-654-3210.&#39;<\/p>\n<p>matches = re.findall(pattern, string)<\/p>\n<p>print(&#39;Phone numbers found:&#39;, matches)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5339\u914d URL<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">pattern = r&#39;https?:\/\/(?:www\\.)?\\w+\\.\\w+(?:\/\\w+)*&#39;<\/p>\n<p>string = &#39;Visit us at https:\/\/www.example.com or http:\/\/example.org.&#39;<\/p>\n<p>matches = re.findall(pattern, string)<\/p>\n<p>print(&#39;URLs found:&#39;, matches)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5339\u914d\u65e5\u671f<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">pattern = r&#39;\\b\\d{4}-\\d{2}-\\d{2}\\b&#39;<\/p>\n<p>string = &#39;The event is scheduled on 2023-10-25.&#39;<\/p>\n<p>match = re.search(pattern, string)<\/p>\n<p>if match:<\/p>\n<p>    print(&#39;Date found:&#39;, match.group())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u516d\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u8c03\u8bd5\u5de5\u5177<\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u8c03\u8bd5\u5de5\u5177\u53ef\u4ee5\u5e2e\u52a9\u60a8\u6d4b\u8bd5\u548c\u8c03\u8bd5\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u7684\u5728\u7ebf\u5de5\u5177\uff1a<\/p>\n<\/p>\n<ol>\n<li><a href=\"https:\/\/regex101.com\/\">Regex101<\/a><\/li>\n<li><a href=\"https:\/\/regexr.com\/\">RegExr<\/a><\/li>\n<li><a href=\"http:\/\/www.regexpal.com\/\">RegexPal<\/a><\/li>\n<\/ol>\n<p><p>\u4e03\u3001\u6027\u80fd\u4f18\u5316<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u9884\u7f16\u8bd1\u6b63\u5219\u8868\u8fbe\u5f0f<\/strong><\/p>\n<\/p>\n<p><p>\u5bf9\u4e8e\u9700\u8981\u9891\u7e41\u4f7f\u7528\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>re.compile()<\/code> \u9884\u7f16\u8bd1\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f\u4ee5\u63d0\u9ad8\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">pattern = re.compile(r&#39;\\d+&#39;)<\/p>\n<p>matches = pattern.findall(&#39;abc123def456&#39;)<\/p>\n<p>print(&#39;Precompiled findall found:&#39;, matches)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u907f\u514d\u4f7f\u7528\u590d\u6742\u7684\u6a21\u5f0f<\/strong><\/p>\n<\/p>\n<p><p>\u7b80\u5316\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f\u53ef\u4ee5\u63d0\u9ad8\u5339\u914d\u901f\u5ea6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u590d\u6742\u6a21\u5f0f<\/p>\n<p>pattern = r&#39;(\\d{3}|\\(\\d{3}\\))(-|\\s)?\\d{3}(-|\\s)\\d{4}&#39;<\/p>\n<h2><strong>\u7b80\u5316\u6a21\u5f0f<\/strong><\/h2>\n<p>pattern = r&#39;\\(?\\d{3}\\)?-?\\s*\\d{3}-?\\s*\\d{4}&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528\u539f\u59cb\u5b57\u7b26\u4e32<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u539f\u59cb\u5b57\u7b26\u4e32\uff08\u5373\u5728\u5b57\u7b26\u4e32\u524d\u52a0 <code>r<\/code>\uff09\u53ef\u4ee5\u907f\u514d\u8f6c\u4e49\u5b57\u7b26\u7684\u53cc\u91cd\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">pattern = r&#39;\\d+&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u516b\u3001\u5e38\u89c1\u95ee\u9898\u548c\u89e3\u51b3\u65b9\u6848<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u4e0d\u5230\u9884\u671f\u7684\u7ed3\u679c<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u68c0\u67e5\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u5426\u6b63\u786e\u4e66\u5199\u3002<\/li>\n<li>\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u8c03\u8bd5\u5de5\u5177\u8fdb\u884c\u6d4b\u8bd5\u3002<\/li>\n<li>\u786e\u4fdd\u5339\u914d\u6a21\u5f0f\u548c\u5b57\u7b26\u4e32\u4e00\u81f4\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u901f\u5ea6\u6162<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u7b80\u5316\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f\u3002<\/li>\n<li>\u9884\u7f16\u8bd1\u6b63\u5219\u8868\u8fbe\u5f0f\u3002<\/li>\n<li>\u907f\u514d\u8fc7\u4e8e\u590d\u6742\u7684\u5339\u914d\u6a21\u5f0f\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u65e0\u6cd5\u5339\u914d\u591a\u884c\u5b57\u7b26\u4e32<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u4f7f\u7528 <code>re.DOTALL<\/code> \u6807\u5fd7\u4f7f <code>.<\/code> \u5339\u914d\u5305\u62ec\u6362\u884c\u7b26\u5728\u5185\u7684\u6240\u6709\u5b57\u7b26\u3002<\/li>\n<li>\u4f7f\u7528 <code>re.MULTILINE<\/code> \u6807\u5fd7\u4f7f <code>^<\/code> \u548c <code>$<\/code> \u5339\u914d\u6bcf\u4e00\u884c\u7684\u5f00\u5934\u548c\u7ed3\u5c3e\u3002<\/li>\n<\/ul>\n<p><pre><code class=\"language-python\">pattern = re.compile(r&#39;^abc&#39;, re.MULTILINE)<\/p>\n<p>string = &#39;abc\\nabc\\nabc&#39;<\/p>\n<p>matches = pattern.findall(string)<\/p>\n<p>print(&#39;Multiline findall found:&#39;, matches)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u60a8\u5e94\u8be5\u5bf9 Python \u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u57fa\u672c\u7528\u6cd5\u548c\u9ad8\u7ea7\u6280\u5de7\u6709\u4e86\u8f83\u4e3a\u5168\u9762\u7684\u4e86\u89e3\u3002\u638c\u63e1\u8fd9\u4e9b\u6280\u80fd\u5c06\u5e2e\u52a9\u60a8\u5728\u5904\u7406\u5b57\u7b26\u4e32\u6570\u636e\u65f6\u66f4\u52a0\u5f97\u5fc3\u5e94\u624b\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\u6a21\u5f0f\u5339\u914d\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>re<\/code>\u6a21\u5757\u6765\u8fdb\u884c\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u64cd\u4f5c\u3002\u901a\u8fc7<code>re.search()<\/code>\u3001<code>re.match()<\/code>\u548c<code>re.findall()<\/code>\u7b49\u51fd\u6570\uff0c\u53ef\u4ee5\u5728\u5b57\u7b26\u4e32\u4e2d\u67e5\u627e\u7279\u5b9a\u7684\u6a21\u5f0f\u3002<code>re.search()<\/code>\u4f1a\u5728\u5b57\u7b26\u4e32\u4e2d\u67e5\u627e\u7b2c\u4e00\u4e2a\u5339\u914d\u9879\uff0c<code>re.match()<\/code>\u4f1a\u4ece\u5b57\u7b26\u4e32\u7684\u8d77\u59cb\u4f4d\u7f6e\u5f00\u59cb\u5339\u914d\uff0c\u800c<code>re.findall()<\/code>\u5219\u4f1a\u8fd4\u56de\u6240\u6709\u5339\u914d\u7684\u7ed3\u679c\u3002\u4f7f\u7528\u8fd9\u4e9b\u51fd\u6570\u65f6\uff0c\u9700\u8981\u4f20\u5165\u6b63\u5219\u8868\u8fbe\u5f0f\u548c\u76ee\u6807\u5b57\u7b26\u4e32\u3002<\/p>\n<p><strong>\u5982\u4f55\u7f16\u5199\u590d\u6742\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u4ee5\u63d0\u9ad8\u5339\u914d\u7cbe\u5ea6\uff1f<\/strong><br \/>\u7f16\u5199\u590d\u6742\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u65f6\uff0c\u53ef\u4ee5\u5229\u7528\u5b57\u7b26\u7c7b\u3001\u91cf\u8bcd\u3001\u5206\u7ec4\u548c\u53cd\u5411\u5f15\u7528\u7b49\u529f\u80fd\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>[a-zA-Z]<\/code>\u6765\u5339\u914d\u5b57\u6bcd\uff0c\u4f7f\u7528<code>+<\/code>\u6765\u8868\u793a\u4e00\u4e2a\u6216\u591a\u4e2a\u5b57\u7b26\uff0c\u4f7f\u7528<code>(...)<\/code>\u8fdb\u884c\u5206\u7ec4\u5339\u914d\u3002\u901a\u8fc7\u7ec4\u5408\u8fd9\u4e9b\u5143\u7d20\uff0c\u53ef\u4ee5\u6784\u5efa\u51fa\u66f4\u7cbe\u786e\u7684\u5339\u914d\u6a21\u5f0f\u3002\u540c\u65f6\uff0c\u4f7f\u7528<code>re.compile()<\/code>\u53ef\u4ee5\u5c06\u6b63\u5219\u8868\u8fbe\u5f0f\u7f16\u8bd1\u6210\u4e00\u4e2a\u5bf9\u8c61\uff0c\u4ee5\u4fbf\u4e8e\u591a\u6b21\u4f7f\u7528\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5904\u7406\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u5339\u914d\u7ed3\u679c\uff1f<\/strong><br \/>\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u5339\u914d\u7ed3\u679c\u901a\u5e38\u662f\u4e00\u4e2a<code>Match<\/code>\u5bf9\u8c61\uff0c\u5176\u4e2d\u5305\u542b\u4e86\u5339\u914d\u7684\u8be6\u7ec6\u4fe1\u606f\u3002\u53ef\u4ee5\u901a\u8fc7<code>group()<\/code>\u65b9\u6cd5\u83b7\u53d6\u5339\u914d\u7684\u5b57\u7b26\u4e32\uff0c\u901a\u8fc7<code>start()<\/code>\u548c<code>end()<\/code>\u65b9\u6cd5\u83b7\u53d6\u5339\u914d\u7684\u8d77\u59cb\u548c\u7ed3\u675f\u4f4d\u7f6e\u3002\u5982\u679c\u4f7f\u7528<code>re.findall()<\/code>\uff0c\u5219\u8fd4\u56de\u7684\u662f\u4e00\u4e2a\u5217\u8868\uff0c\u5305\u542b\u4e86\u6240\u6709\u5339\u914d\u7684\u7ed3\u679c\u3002\u7406\u89e3\u8fd9\u4e9b\u8fd4\u56de\u503c\u6709\u52a9\u4e8e\u5bf9\u5339\u914d\u7ed3\u679c\u8fdb\u884c\u540e\u7eed\u5904\u7406\u548c\u5206\u6790\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python \u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u4f7f\u7528\u975e\u5e38\u65b9\u4fbf\u3001\u529f\u80fd\u5f3a\u5927\u3001\u80fd\u591f\u5904\u7406\u590d\u6742\u7684\u5b57\u7b26\u4e32\u5339\u914d\u9700\u6c42\u3002 \u9996\u5148\uff0c\u8981\u4f7f\u7528 Python [&hellip;]","protected":false},"author":3,"featured_media":1080720,"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\/1080709"}],"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=1080709"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1080709\/revisions"}],"predecessor-version":[{"id":1080722,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1080709\/revisions\/1080722"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1080720"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1080709"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1080709"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1080709"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}