{"id":2799,"date":"2025-03-15T19:37:10","date_gmt":"2025-03-15T11:37:10","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=2799"},"modified":"2025-03-15T19:37:10","modified_gmt":"2025-03-15T11:37:10","slug":"python3-%e6%ad%a3%e5%88%99%e8%a1%a8%e8%be%be%e5%bc%8f%e8%af%a6%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/03\/15\/python3-%e6%ad%a3%e5%88%99%e8%a1%a8%e8%be%be%e5%bc%8f%e8%af%a6%e8%a7%a3\/","title":{"rendered":"Python3 \u6b63\u5219\u8868\u8fbe\u5f0f\u8be6\u89e3"},"content":{"rendered":"\n<p>Python \u7684 <code>re<\/code> \u6a21\u5757\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u529f\u80fd\uff0c\u9002\u7528\u4e8e\u5b57\u7b26\u4e32\u5339\u914d\u3001\u641c\u7d22\u3001\u66ff\u6362\u7b49\u64cd\u4f5c\u3002\u4e0b\u9762\u662f Python3 \u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u6df1\u5165\u5b66\u4e60\u5185\u5bb9\uff1a<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>1. \u5bfc\u5165 <code>re<\/code> \u6a21\u5757<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>import re<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>2. \u6b63\u5219\u8868\u8fbe\u5f0f\u57fa\u7840<\/strong><\/h2>\n\n\n\n<p>\u6b63\u5219\u8868\u8fbe\u5f0f\u7528\u4e8e\u5339\u914d\u7279\u5b9a\u7684\u5b57\u7b26\u4e32\u6a21\u5f0f\uff0c\u4f8b\u5982\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>\\d<\/code> \u5339\u914d\u6570\u5b57<\/li>\n\n\n\n<li><code>\\w<\/code> \u5339\u914d\u5b57\u6bcd\u3001\u6570\u5b57\u548c\u4e0b\u5212\u7ebf<\/li>\n\n\n\n<li><code>.<\/code> \u5339\u914d\u4efb\u610f\u5b57\u7b26\uff08\u6362\u884c\u7b26\u9664\u5916\uff09<\/li>\n\n\n\n<li><code>*<\/code> \u5339\u914d 0 \u6216\u66f4\u591a\u4e2a\u524d\u9762\u7684\u5b57\u7b26<\/li>\n\n\n\n<li><code>+<\/code> \u5339\u914d 1 \u6216\u66f4\u591a\u4e2a\u524d\u9762\u7684\u5b57\u7b26<\/li>\n\n\n\n<li><code>?<\/code> \u5339\u914d 0 \u6216 1 \u4e2a\u524d\u9762\u7684\u5b57\u7b26<\/li>\n\n\n\n<li><code>{n,m}<\/code> \u5339\u914d n \u5230 m \u6b21<\/li>\n\n\n\n<li><code>^<\/code> \u5339\u914d\u5b57\u7b26\u4e32\u7684\u5f00\u59cb<\/li>\n\n\n\n<li><code>$<\/code> \u5339\u914d\u5b57\u7b26\u4e32\u7684\u7ed3\u5c3e<\/li>\n<\/ul>\n\n\n\n<p>\u793a\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pattern = r'\\d+'  # \u5339\u914d\u4e00\u4e2a\u6216\u591a\u4e2a\u6570\u5b57\ntext = \"\u4eca\u5929\u662f 2025 \u5e74 3 \u6708 15 \u65e5\"\nmatch = re.findall(pattern, text)\nprint(match)  # \u8f93\u51fa\uff1a&#91;'2025', '3', '15']<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>3. <code>re<\/code> \u6a21\u5757\u4e3b\u8981\u65b9\u6cd5<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3.1 <code>re.match()<\/code><\/strong><\/h3>\n\n\n\n<p>\u4ece\u5b57\u7b26\u4e32\u5f00\u5934\u5339\u914d\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u5982\u679c\u5339\u914d\u6210\u529f\uff0c\u5219\u8fd4\u56de\u5339\u914d\u5bf9\u8c61\uff0c\u5426\u5219\u8fd4\u56de <code>None<\/code>\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pattern = r'hello'\ntext = \"hello world\"\nmatch = re.match(pattern, text)\nif match:\n    print(\"\u5339\u914d\u6210\u529f:\", match.group())  # \u8f93\u51fa\uff1ahello<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3.2 <code>re.search()<\/code><\/strong><\/h3>\n\n\n\n<p>\u5728\u6574\u4e2a\u5b57\u7b26\u4e32\u4e2d\u641c\u7d22\u7b2c\u4e00\u4e2a\u5339\u914d\u9879\uff0c\u8fd4\u56de\u5339\u914d\u5bf9\u8c61\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pattern = r'\\d+'\ntext = \"\u4eca\u5929\u662f 2025 \u5e74\"\nmatch = re.search(pattern, text)\nif match:\n    print(\"\u627e\u5230\u6570\u5b57:\", match.group())  # \u8f93\u51fa\uff1a2025<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3.3 <code>re.findall()<\/code><\/strong><\/h3>\n\n\n\n<p>\u8fd4\u56de\u6240\u6709\u5339\u914d\u7684\u5b57\u7b26\u4e32\u5217\u8868\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pattern = r'\\d+'\ntext = \"2025 \u5e74 3 \u6708 15 \u65e5\"\nmatches = re.findall(pattern, text)\nprint(matches)  # \u8f93\u51fa\uff1a&#91;'2025', '3', '15']<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3.4 <code>re.finditer()<\/code><\/strong><\/h3>\n\n\n\n<p>\u8fd4\u56de\u4e00\u4e2a\u8fed\u4ee3\u5668\uff0c\u6bcf\u4e2a\u5143\u7d20\u662f\u4e00\u4e2a <code>match<\/code> \u5bf9\u8c61\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pattern = r'\\d+'\ntext = \"2025 \u5e74 3 \u6708 15 \u65e5\"\nmatches = re.finditer(pattern, text)\nfor match in matches:\n    print(\"\u627e\u5230\u6570\u5b57:\", match.group())  # \u9010\u4e2a\u8f93\u51fa 2025, 3, 15<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3.5 <code>re.sub()<\/code><\/strong><\/h3>\n\n\n\n<p>\u66ff\u6362\u5b57\u7b26\u4e32\u4e2d\u7684\u5339\u914d\u5185\u5bb9\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pattern = r'\\d+'\ntext = \"2025 \u5e74 3 \u6708 15 \u65e5\"\nnew_text = re.sub(pattern, \"XX\", text)\nprint(new_text)  # \u8f93\u51fa\uff1aXX \u5e74 XX \u6708 XX \u65e5<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3.6 <code>re.split()<\/code><\/strong><\/h3>\n\n\n\n<p>\u4f7f\u7528\u5339\u914d\u7684\u5185\u5bb9\u62c6\u5206\u5b57\u7b26\u4e32\uff0c\u8fd4\u56de\u5217\u8868\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pattern = r'\\s+'\ntext = \"hello   world  python\"\nresult = re.split(pattern, text)\nprint(result)  # \u8f93\u51fa\uff1a&#91;'hello', 'world', 'python']<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>4. \u6b63\u5219\u8868\u8fbe\u5f0f\u8fdb\u9636<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4.1 \u7ec4 (<code>()<\/code>\u7684\u4f5c\u7528)<\/strong><\/h3>\n\n\n\n<p>\u53ef\u4ee5\u4f7f\u7528 <code>()<\/code> \u521b\u5efa\u5339\u914d\u7ec4\uff0c\u5e76\u4f7f\u7528 <code>.group(n)<\/code> \u63d0\u53d6\u5185\u5bb9\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pattern = r'(\\d{4})-(\\d{2})-(\\d{2})'\ntext = \"\u65e5\u671f\u662f 2025-03-15\"\nmatch = re.search(pattern, text)\nif match:\n    print(\"\u5e74\u4efd:\", match.group(1))  # 2025\n    print(\"\u6708\u4efd:\", match.group(2))  # 03\n    print(\"\u65e5\u671f:\", match.group(3))  # 15<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4.2 \u975e\u8d2a\u5a6a\u5339\u914d<\/strong><\/h3>\n\n\n\n<p>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c<code>*<\/code> \u548c <code>+<\/code> \u662f\u8d2a\u5a6a\u7684\uff08\u5c3d\u53ef\u80fd\u591a\u5730\u5339\u914d\uff09\u3002<br>\u4f7f\u7528 <code>?<\/code> \u8ba9\u5b83\u53d8\u4e3a\u975e\u8d2a\u5a6a\u5339\u914d\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>text = '&lt;div&gt;hello&lt;\/div&gt;&lt;div&gt;world&lt;\/div&gt;'\ngreedy_match = re.search(r'&lt;div&gt;.*&lt;\/div&gt;', text)\nlazy_match = re.search(r'&lt;div&gt;.*?&lt;\/div&gt;', text)\n\nprint(\"\u8d2a\u5a6a\u5339\u914d:\", greedy_match.group())  # \u8f93\u51fa\uff1a&lt;div&gt;hello&lt;\/div&gt;&lt;div&gt;world&lt;\/div&gt;\nprint(\"\u975e\u8d2a\u5a6a\u5339\u914d:\", lazy_match.group())  # \u8f93\u51fa\uff1a&lt;div&gt;hello&lt;\/div&gt;<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4.3 <code>re.compile()<\/code><\/strong><\/h3>\n\n\n\n<p>\u9884\u7f16\u8bd1\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u63d0\u9ad8\u5339\u914d\u6548\u7387\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pattern = re.compile(r'\\d+')\ntext = \"\u4eca\u5929\u662f 2025 \u5e74 3 \u6708 15 \u65e5\"\nmatches = pattern.findall(text)\nprint(matches)  # &#91;'2025', '3', '15']<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4.4 <code>re.VERBOSE<\/code> \u6a21\u5f0f<\/strong><\/h3>\n\n\n\n<p>\u5141\u8bb8\u4f7f\u7528\u591a\u884c\u7f16\u5199\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u5e76\u6dfb\u52a0\u6ce8\u91ca\uff0c\u63d0\u9ad8\u53ef\u8bfb\u6027\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pattern = re.compile(r\"\"\"\n    (\\d{4})  # \u5e74\u4efd\n    -        # \u8fde\u63a5\u7b26\n    (\\d{2})  # \u6708\u4efd\n    -        # \u8fde\u63a5\u7b26\n    (\\d{2})  # \u65e5\u671f\n\"\"\", re.VERBOSE)\n\ntext = \"\u4eca\u5929\u7684\u65e5\u671f\u662f 2025-03-15\"\nmatch = pattern.search(text)\nif match:\n    print(match.groups())  # ('2025', '03', '15')<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>5. \u5e38\u89c1\u6b63\u5219\u8868\u8fbe\u5f0f\u5e94\u7528<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>5.1 \u5339\u914d\u624b\u673a\u53f7<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>pattern = r'1&#91;3-9]\\d{9}'\ntext = \"\u6211\u7684\u624b\u673a\u53f7\u7801\u662f 13812345678\"\nmatch = re.search(pattern, text)\nif match:\n    print(\"\u5339\u914d\u5230\u624b\u673a\u53f7:\", match.group())  # 13812345678<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>5.2 \u5339\u914d\u90ae\u7bb1<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>pattern = r'&#91;a-zA-Z0-9._%+-]+@&#91;a-zA-Z0-9.-]+\\.&#91;a-zA-Z]{2,}'\ntext = \"\u8054\u7cfb\u6211\uff1aexample@gmail.com\"\nmatch = re.search(pattern, text)\nif match:\n    print(\"\u5339\u914d\u5230\u90ae\u7bb1:\", match.group())  # example@gmail.com<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>5.3 \u63d0\u53d6 URL<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>pattern = r'https?:\/\/&#91;a-zA-Z0-9.\/-]+'\ntext = \"\u8bbf\u95ee\u6211\u7684\u535a\u5ba2\uff1ahttps:\/\/www.example.com\"\nmatch = re.search(pattern, text)\nif match:\n    print(\"\u5339\u914d\u5230 URL:\", match.group())  # https:\/\/www.example.com<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Python3 \u6b63\u5219\u8868\u8fbe\u5f0f\u6df1\u5165\u5b66\u4e60\uff08\u8fdb\u9636\u7bc7\uff09<\/strong><\/h3>\n\n\n\n<p>\u5728\u57fa\u7840\u77e5\u8bc6\u7684\u5b66\u4e60\u4e4b\u540e\uff0c\u6211\u4eec\u7ee7\u7eed\u6df1\u5165\u63a2\u8ba8 Python3 \u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u9ad8\u7ea7\u5e94\u7528\uff0c\u5305\u62ec\u9ad8\u7ea7\u5339\u914d\u6a21\u5f0f\u3001\u65ad\u8a00\u3001\u96f6\u5bbd\u65ad\u8a00\u3001\u8d1f\u5411\u5339\u914d\u7b49\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>6. <code>re<\/code> \u6a21\u5757\u7684\u9ad8\u7ea7\u7528\u6cd5<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>6.1 <code>re.MULTILINE<\/code>\uff08\u591a\u884c\u6a21\u5f0f\uff09<\/strong><\/h3>\n\n\n\n<p><strong>\u9ed8\u8ba4\u60c5\u51b5\u4e0b<\/strong>\uff0c<code>^<\/code> \u4ec5\u5339\u914d\u5b57\u7b26\u4e32\u7684\u5f00\u5934\uff0c<code>$<\/code> \u4ec5\u5339\u914d\u5b57\u7b26\u4e32\u7684\u7ed3\u5c3e\u3002\u4f46\u5982\u679c\u542f\u7528\u4e86 <code>re.MULTILINE<\/code>\uff0c\u5219 <code>^<\/code> \u548c <code>$<\/code> \u8fd8\u4f1a\u5339\u914d\u6bcf\u4e00\u884c\u7684\u5f00\u5934\u548c\u7ed3\u5c3e\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import re\n\ntext = \"\"\"Hello World\nPython is great\nRegular Expressions\"\"\"\n\npattern = r\"^Python\"\nmatch = re.search(pattern, text, re.MULTILINE)\n\nif match:\n    print(\"\u5339\u914d\u6210\u529f:\", match.group())  # \u8f93\u51fa\uff1aPython<\/code><\/pre>\n\n\n\n<p>\ud83d\udd39 <strong>\u5982\u679c\u4e0d\u52a0 <code>re.MULTILINE<\/code>\uff0c\u9ed8\u8ba4 <code>^<\/code> \u53ea\u5339\u914d\u6574\u4e2a\u5b57\u7b26\u4e32\u7684\u5f00\u5934\uff0c\u800c\u4e0d\u4f1a\u5339\u914d\u7b2c\u4e8c\u884c\u7684 <code>Python<\/code>\u3002<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>6.2 <code>re.IGNORECASE<\/code>\uff08\u5ffd\u7565\u5927\u5c0f\u5199\u5339\u914d\uff09<\/strong><\/h3>\n\n\n\n<p>\u5ffd\u7565\u5b57\u6bcd\u5927\u5c0f\u5199\uff0c\u4f7f\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u5339\u914d\u66f4\u52a0\u7075\u6d3b\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pattern = r\"hello\"\ntext = \"Hello World\"\n\nmatch = re.search(pattern, text, re.IGNORECASE)\nif match:\n    print(\"\u5339\u914d\u6210\u529f:\", match.group())  # \u8f93\u51fa\uff1aHello<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>6.3 <code>re.DOTALL<\/code>\uff08\u8ba9 <code>.<\/code> \u5339\u914d\u6362\u884c\u7b26\uff09<\/strong><\/h3>\n\n\n\n<p>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c<code>.<\/code> \u4e0d\u80fd\u5339\u914d\u6362\u884c\u7b26 <code>\\n<\/code>\u3002\u4f7f\u7528 <code>re.DOTALL<\/code> \u8ba9 <code>.<\/code> \u53ef\u4ee5\u5339\u914d\u6362\u884c\u7b26\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pattern = r\"Hello.*World\"\ntext = \"Hello\\nWorld\"\n\nmatch = re.search(pattern, text, re.DOTALL)\nif match:\n    print(\"\u5339\u914d\u6210\u529f:\", match.group())  # \u8f93\u51fa\uff1aHello\\nWorld<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>7. \u65ad\u8a00\uff08Assertions\uff09<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>7.1 \u5148\u884c\u65ad\u8a00\uff08Lookahead\uff09<\/strong><\/h3>\n\n\n\n<p>\u5148\u884c\u65ad\u8a00 (<code>?=<\/code>) \u7528\u4e8e\u5339\u914d\u67d0\u4e2a\u6a21\u5f0f <strong>\u524d\u9762<\/strong> \u7684\u6587\u672c\uff0c\u800c\u4e0d\u4f1a\u6d88\u8017\u5b57\u7b26\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pattern = r'\\d+(?=\u5143)'\ntext = \"\u5546\u54c1\u4ef7\u683c\u662f 100\u5143 \u548c 200\u5143\"\n\nmatches = re.findall(pattern, text)\nprint(matches)  # \u8f93\u51fa\uff1a&#91;'100', '200']<\/code><\/pre>\n\n\n\n<p>\ud83d\udd39 <strong>\u89e3\u91ca\uff1a<\/strong> <code>\\d+<\/code> \u4ee3\u8868\u5339\u914d\u4e00\u4e2a\u6216\u591a\u4e2a\u6570\u5b57\uff0c<code>(?=\u5143)<\/code> \u4ee3\u8868\u8fd9\u4e9b\u6570\u5b57\u540e\u9762\u5fc5\u987b\u662f\u201c\u5143\u201d\uff0c\u4f46\u201c\u5143\u201d\u4e0d\u4f1a\u88ab\u5339\u914d\u51fa\u6765\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>7.2 \u8d1f\u5411\u5148\u884c\u65ad\u8a00\uff08Negative Lookahead\uff09<\/strong><\/h3>\n\n\n\n<p>\u8d1f\u5411\u5148\u884c\u65ad\u8a00 (<code>?!<\/code>) \u4ec5\u5339\u914d\u4e0d\u7b26\u5408\u67d0\u79cd\u6a21\u5f0f\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pattern = r'hello(?! world)'\ntext1 = \"hello world\"\ntext2 = \"hello python\"\n\nmatch1 = re.search(pattern, text1)\nmatch2 = re.search(pattern, text2)\n\nprint(\"\u5339\u914d\u7ed3\u679c1:\", bool(match1))  # False\nprint(\"\u5339\u914d\u7ed3\u679c2:\", bool(match2))  # True<\/code><\/pre>\n\n\n\n<p>\ud83d\udd39 <strong>\u89e3\u91ca\uff1a<\/strong> <code>hello(?! world)<\/code> \u4ee3\u8868\u5339\u914d <code>hello<\/code>\uff0c\u4f46\u540e\u9762\u4e0d\u80fd\u8ddf <code>world<\/code>\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>7.3 \u540e\u884c\u65ad\u8a00\uff08Lookbehind\uff09<\/strong><\/h3>\n\n\n\n<p>\u540e\u884c\u65ad\u8a00 (<code>?&lt;=<\/code>) \u7528\u4e8e\u5339\u914d\u67d0\u4e2a\u6a21\u5f0f <strong>\u540e\u9762<\/strong> \u7684\u6587\u672c\uff0c\u800c\u4e0d\u4f1a\u6d88\u8017\u5b57\u7b26\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pattern = r'(?&lt;=\\$)\\d+'\ntext = \"\u4ef7\u683c\u662f $100 \u548c $200\"\n\nmatches = re.findall(pattern, text)\nprint(matches)  # \u8f93\u51fa\uff1a&#91;'100', '200']<\/code><\/pre>\n\n\n\n<p>\ud83d\udd39 <strong>\u89e3\u91ca\uff1a<\/strong> <code>(?&lt;=\\$)<\/code> \u4ee3\u8868\u5339\u914d <code>$<\/code> \u540e\u9762\u7684\u6570\u5b57\uff0c<code>$<\/code> \u4e0d\u4f1a\u88ab\u5339\u914d\u51fa\u6765\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>7.4 \u8d1f\u5411\u540e\u884c\u65ad\u8a00\uff08Negative Lookbehind\uff09<\/strong><\/h3>\n\n\n\n<p>\u8d1f\u5411\u540e\u884c\u65ad\u8a00 (<code>?&lt;!<\/code>) \u4ec5\u5339\u914d <strong>\u524d\u9762\u4e0d\u7b26\u5408<\/strong> \u67d0\u79cd\u6a21\u5f0f\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pattern = r'(?&lt;!\\$)\\d+'\ntext = \"100 \u662f\u666e\u901a\u6570\u5b57\uff0c$200 \u662f\u5e26\u7b26\u53f7\u7684\u91d1\u989d\"\n\nmatches = re.findall(pattern, text)\nprint(matches)  # \u8f93\u51fa\uff1a&#91;'100']<\/code><\/pre>\n\n\n\n<p>\ud83d\udd39 <strong>\u89e3\u91ca\uff1a<\/strong> <code>(?&lt;!\\$)\\d+<\/code> \u4ee3\u8868\u5339\u914d\u4e0d\u5728 <code>$<\/code> \u540e\u9762\u7684\u6570\u5b57\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>8. \u8d2a\u5a6a\u4e0e\u975e\u8d2a\u5a6a\u5339\u914d\uff08Greedy vs Non-Greedy\uff09<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>8.1 \u8d2a\u5a6a\u5339\u914d<\/strong><\/h3>\n\n\n\n<p>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c<code>*<\/code>, <code>+<\/code>, <code>{}<\/code> \u662f<strong>\u8d2a\u5a6a\u5339\u914d<\/strong>\uff0c\u4f1a\u5339\u914d\u5c3d\u53ef\u80fd\u591a\u7684\u5b57\u7b26\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>text = \"&lt;div&gt;hello&lt;\/div&gt;&lt;div&gt;world&lt;\/div&gt;\"\npattern = r\"&lt;div&gt;.*&lt;\/div&gt;\"\n\nmatch = re.search(pattern, text)\nprint(match.group())  # \u8f93\u51fa\uff1a&lt;div&gt;hello&lt;\/div&gt;&lt;div&gt;world&lt;\/div&gt;<\/code><\/pre>\n\n\n\n<p>\ud83d\udd39 <strong>\u89e3\u91ca\uff1a<\/strong> <code>.*<\/code> \u8bd5\u56fe\u5339\u914d\u5c3d\u53ef\u80fd\u591a\u7684\u5b57\u7b26\uff0c\u56e0\u6b64\u5339\u914d\u4e86<strong>\u6574\u4e2a<\/strong> <code>&lt;div&gt;hello&lt;\/div&gt;&lt;div&gt;world&lt;\/div&gt;<\/code>\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>8.2 \u975e\u8d2a\u5a6a\u5339\u914d<\/strong><\/h3>\n\n\n\n<p>\u901a\u8fc7 <code>*?<\/code>, <code>+?<\/code>, <code>{n,m}?<\/code> \u8ba9\u5339\u914d\u53d8\u5f97<strong>\u975e\u8d2a\u5a6a<\/strong>\uff0c\u5373\u5339\u914d\u5c3d\u53ef\u80fd\u5c11\u7684\u5b57\u7b26\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pattern = r\"&lt;div&gt;.*?&lt;\/div&gt;\"\nmatch = re.search(pattern, text)\nprint(match.group())  # \u8f93\u51fa\uff1a&lt;div&gt;hello&lt;\/div&gt;<\/code><\/pre>\n\n\n\n<p>\ud83d\udd39 <strong>\u89e3\u91ca\uff1a<\/strong> <code>.*?<\/code> \u4ec5\u5339\u914d\u5230<strong>\u7b2c\u4e00\u4e2a<\/strong> <code>&lt;\/div&gt;<\/code>\uff0c\u6240\u4ee5\u8fd4\u56de <code>&lt;div&gt;hello&lt;\/div&gt;<\/code>\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>9. \u590d\u6742\u6848\u4f8b<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>9.1 \u63d0\u53d6 HTML \u6807\u7b7e\u5185\u5bb9<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>pattern = r'&lt;title&gt;(.*?)&lt;\/title&gt;'\ntext = \"&lt;html&gt;&lt;head&gt;&lt;title&gt;Python \u6b63\u5219\u8868\u8fbe\u5f0f&lt;\/title&gt;&lt;\/head&gt;&lt;\/html&gt;\"\n\nmatch = re.search(pattern, text)\nif match:\n    print(match.group(1))  # \u8f93\u51fa\uff1aPython \u6b63\u5219\u8868\u8fbe\u5f0f<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>9.2 \u89e3\u6790 URL<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>pattern = r\"https?:\/\/(www\\.)?(&#91;\\w.-]+)\"\ntext = \"\u8bbf\u95ee https:\/\/www.example.com \u6216 http:\/\/test.com\"\n\nmatches = re.findall(pattern, text)\nprint(matches)  \n# \u8f93\u51fa\uff1a&#91;('www.', 'example.com'), ('', 'test.com')]<\/code><\/pre>\n\n\n\n<p>\ud83d\udd39 <strong>\u89e3\u91ca\uff1a<\/strong> \u8fd9\u91cc\u63d0\u53d6\u4e86 URL \u7684\u57df\u540d\u90e8\u5206\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>9.3 \u66ff\u6362\u654f\u611f\u8bcd<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>pattern = r'\u574f\u8bdd|\u810f\u8bdd'\ntext = \"\u8fd9\u4e2a\u4eba\u8bf4\u4e86\u4e00\u4e9b\u574f\u8bdd\u548c\u810f\u8bdd\"\nnew_text = re.sub(pattern, \"**\", text)\nprint(new_text)  # \u8f93\u51fa\uff1a\u8fd9\u4e2a\u4eba\u8bf4\u4e86\u4e00\u4e9b**\u548c**<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u5f53\u7136\uff01Python3 \u6b63\u5219\u8868\u8fbe\u5f0f\u8fd8\u6709\u8bb8\u591a\u66f4\u9ad8\u7ea7\u7684\u7528\u6cd5\uff0c\u4f8b\u5982\uff1a\u56de\u6eaf\u5f15\u7528\u3001\u5d4c\u5957\u5339\u914d\u3001\u52a8\u6001\u7f16\u8bd1\u3001\u6027\u80fd\u4f18\u5316\u3001\u4e0e <code>regex<\/code> \u6a21\u5757\u7684\u533a\u522b\u7b49\u3002\u6211\u4eec\u7ee7\u7eed\u6df1\u5165\u5b66\u4e60\uff01\ud83d\ude80<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>11. \u56de\u6eaf\u5f15\u7528\uff08Backreferences\uff09<\/strong><\/h2>\n\n\n\n<p><strong>\u56de\u6eaf\u5f15\u7528\uff08Backreference\uff09<\/strong> \u5141\u8bb8\u5728\u5339\u914d\u6a21\u5f0f\u4e2d\u5f15\u7528\u4e4b\u524d\u5339\u914d\u7684\u7ec4\uff0c\u5e38\u7528\u4e8e\u68c0\u6d4b\u91cd\u590d\u5355\u8bcd\u3001HTML \u6807\u7b7e\u5339\u914d\u7b49\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>11.1 \u68c0\u6d4b\u91cd\u590d\u5355\u8bcd<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>import re\n\ntext = \"I love love Python!\"\npattern = r\"\\b(\\w+)\\s+\\1\\b\"\n\nmatch = re.search(pattern, text)\nif match:\n    print(\"\u627e\u5230\u91cd\u590d\u5355\u8bcd:\", match.group())  # \u8f93\u51fa\uff1alove love<\/code><\/pre>\n\n\n\n<p>\ud83d\udd39 <strong>\u89e3\u91ca\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>(\\w+)<\/code> \u6355\u83b7\u4e00\u4e2a\u5355\u8bcd<\/li>\n\n\n\n<li><code>\\1<\/code> \u8868\u793a\u5f15\u7528 <strong>\u7b2c 1 \u4e2a\u6355\u83b7\u7ec4<\/strong><\/li>\n\n\n\n<li><code>\\s+<\/code> \u4ee3\u8868\u5355\u8bcd\u4e4b\u95f4\u6709\u7a7a\u683c<\/li>\n\n\n\n<li><code>\\b<\/code> \u8868\u793a\u5355\u8bcd\u8fb9\u754c<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>11.2 HTML \u6807\u7b7e\u7684\u5b8c\u6574\u5339\u914d<\/strong><\/h3>\n\n\n\n<p>\u56de\u6eaf\u5f15\u7528\u53ef\u4ee5\u786e\u4fdd\u8d77\u59cb\u548c\u7ed3\u675f\u6807\u7b7e\u76f8\u540c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>text = \"&lt;b&gt;Python&lt;\/b&gt; and &lt;i&gt;Regex&lt;\/i&gt;\"\npattern = r\"&lt;(\\w+)&gt;(.*?)&lt;\/\\1&gt;\"\n\nmatches = re.findall(pattern, text)\nprint(matches)  \n# \u8f93\u51fa\uff1a&#91;('b', 'Python'), ('i', 'Regex')]<\/code><\/pre>\n\n\n\n<p>\ud83d\udd39 <strong>\u89e3\u91ca\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>&lt;(\\w+)><\/code> \u6355\u83b7 HTML \u6807\u7b7e\u540d<\/li>\n\n\n\n<li><code>.*?<\/code> \u6355\u83b7\u6807\u7b7e\u5185\u90e8\u5185\u5bb9\uff08\u975e\u8d2a\u5a6a\u6a21\u5f0f\uff09<\/li>\n\n\n\n<li><code>&lt;\/\\1><\/code> \u786e\u4fdd\u7ed3\u675f\u6807\u7b7e\u4e0e\u8d77\u59cb\u6807\u7b7e\u5339\u914d<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>12. \u5d4c\u5957\u5339\u914d\uff08\u9012\u5f52\u5339\u914d\uff09<\/strong><\/h2>\n\n\n\n<p>Python \u7684 <code>re<\/code> \u6a21\u5757<strong>\u4e0d\u652f\u6301<\/strong>\u76f4\u63a5\u7684\u9012\u5f52\u5339\u914d\uff0c\u4f8b\u5982\u5d4c\u5957\u62ec\u53f7 <code>((a+b)*c)<\/code>\u3002\u4f46\u662f\u53ef\u4ee5\u4f7f\u7528 <code>regex<\/code> \u6a21\u5757\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import regex\n\ntext = \"(a (b c) d (e (f g)))\"\npattern = r\"\\((?:&#91;^()]++|(?R))*+\\)\"\n\nmatches = regex.findall(pattern, text)\nprint(matches)  # \u8f93\u51fa\uff1a&#91;'(b c)', '(e (f g))', '(a (b c) d (e (f g)))']<\/code><\/pre>\n\n\n\n<p>\ud83d\udd39 <strong>\u89e3\u91ca\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>(?R)<\/code> \u4ee3\u8868<strong>\u9012\u5f52\u8c03\u7528\u81ea\u8eab<\/strong><\/li>\n\n\n\n<li><code>(?:[^()]++|(?R))*+<\/code> \u4ee3\u8868\u5339\u914d\u65e0\u62ec\u53f7\u5185\u5bb9\uff0c\u6216\u9012\u5f52\u5339\u914d\u62ec\u53f7\u5185\u90e8\u7684\u5185\u5bb9<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>13. \u7f16\u8bd1\u6b63\u5219\u8868\u8fbe\u5f0f\uff08<code>re.compile()<\/code>\uff09<\/strong><\/h2>\n\n\n\n<p>\u5f53\u6b63\u5219\u8868\u8fbe\u5f0f\u9700\u8981<strong>\u591a\u6b21\u4f7f\u7528<\/strong>\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>re.compile()<\/code> \u63d0\u524d\u7f16\u8bd1\uff0c\u63d0\u9ad8\u6548\u7387\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pattern = re.compile(r\"\\d{3}-\\d{4}-\\d{4}\")\n\n# \u4f7f\u7528\u5df2\u7f16\u8bd1\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\nprint(pattern.findall(\"\u7535\u8bdd: 123-4567-8910, 987-6543-2100\"))\n# \u8f93\u51fa\uff1a&#91;'123-4567-8910', '987-6543-2100']<\/code><\/pre>\n\n\n\n<p>\ud83d\udd39 <strong>\u4f18\u52bf\uff1a<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u63d0\u9ad8\u6267\u884c\u6548\u7387<\/strong>\uff08\u907f\u514d\u6bcf\u6b21\u90fd\u91cd\u65b0\u89e3\u6790\u6b63\u5219\u8868\u8fbe\u5f0f\uff09<\/li>\n\n\n\n<li><strong>\u4ee3\u7801\u66f4\u6e05\u6670<\/strong>\uff08\u53ef\u4ee5\u5c06\u590d\u6742\u6a21\u5f0f\u5b58\u50a8\u4e3a\u53d8\u91cf\uff09<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>14. <code>re.sub()<\/code> \u8fdb\u9636\u66ff\u6362<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>14.1 \u4f7f\u7528\u56de\u8c03\u51fd\u6570 <code>re.sub()<\/code><\/strong><\/h3>\n\n\n\n<p>\u5982\u679c\u66ff\u6362\u89c4\u5219\u8f83\u590d\u6742\uff0c\u53ef\u4ee5\u4f7f\u7528 <strong>\u56de\u8c03\u51fd\u6570<\/strong> \u5904\u7406\u5339\u914d\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def mask_email(match):\n    return match.group(1) + \"@****.com\"\n\npattern = r\"(\\w+)@\\w+\\.\\w+\"\ntext = \"\u90ae\u7bb1: alice@example.com, bob@gmail.com\"\nmasked_text = re.sub(pattern, mask_email, text)\n\nprint(masked_text)  \n# \u8f93\u51fa\uff1a\u90ae\u7bb1: alice@****.com, bob@****.com<\/code><\/pre>\n\n\n\n<p>\ud83d\udd39 <strong>\u89e3\u91ca\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>re.sub()<\/code> \u5141\u8bb8\u4f7f\u7528\u81ea\u5b9a\u4e49\u51fd\u6570\u8fdb\u884c\u66ff\u6362<\/li>\n\n\n\n<li><code>match.group(1)<\/code> \u83b7\u53d6 <code>@<\/code> \u4e4b\u524d\u7684\u90e8\u5206\uff0c<code>****.com<\/code> \u9690\u85cf\u57df\u540d<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>15. <code>regex<\/code> \u6a21\u5757 VS <code>re<\/code> \u6a21\u5757<\/strong><\/h2>\n\n\n\n<p>Python \u5185\u7f6e\u7684 <code>re<\/code> \u6a21\u5757\u867d\u7136\u529f\u80fd\u5f3a\u5927\uff0c\u4f46 <code>regex<\/code> \u6a21\u5757\u63d0\u4f9b\u66f4\u591a\u529f\u80fd\uff0c\u5982\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th><strong>\u529f\u80fd<\/strong><\/th><th><strong><code>re<\/code> \u6a21\u5757<\/strong><\/th><th><strong><code>regex<\/code> \u6a21\u5757<\/strong><\/th><\/tr><\/thead><tbody><tr><td>Unicode \u652f\u6301<\/td><td>\u2705<\/td><td>\u2705<\/td><\/tr><tr><td>\u9012\u5f52\u5339\u914d<\/td><td>\u274c<\/td><td>\u2705<\/td><\/tr><tr><td>\u53d8\u91cf\u547d\u540d\u7ec4<\/td><td>\u2705<\/td><td>\u2705<\/td><\/tr><tr><td><code>\\N{Unicode\u540d}<\/code><\/td><td>\u274c<\/td><td>\u2705<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u4f8b\u5982 <code>regex<\/code> \u6a21\u5757\u652f\u6301\u547d\u540d Unicode\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import regex\n\ntext = \"\ud835\udcdf\ud835\udcce\ud835\udcc9\ud835\udcbd\ud835\udc5c\ud835\udcc3 \ud835\udcbe\ud835\udcc8 \ud835\udc9c\ud835\udccc\ud835\udc52\ud835\udcc8\ud835\udc5c\ud835\udcc2\ud835\udc52!\"\npattern = r\"\\p{Letter}+\"  # \u5339\u914d\u6240\u6709 Unicode \u6587\u5b57\u5b57\u7b26\n\nmatches = regex.findall(pattern, text)\nprint(matches)  # &#91;'\ud835\udcdf\ud835\udcce\ud835\udcc9\ud835\udcbd\ud835\udc5c\ud835\udcc3', '\ud835\udcbe\ud835\udcc8', '\ud835\udc9c\ud835\udccc\ud835\udc52\ud835\udcc8\ud835\udc5c\ud835\udcc2\ud835\udc52']<\/code><\/pre>\n\n\n\n<p>\ud83d\udd39 <strong>\u89e3\u91ca<\/strong>\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>\\p{Letter}<\/code> \u7528\u4e8e\u5339\u914d\u6240\u6709\u5b57\u6bcd\u5b57\u7b26\uff08\u5305\u62ec Unicode\uff09<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>16. \u6b63\u5219\u8868\u8fbe\u5f0f\u6027\u80fd\u4f18\u5316<\/strong><\/h2>\n\n\n\n<p>\u6b63\u5219\u8868\u8fbe\u5f0f\u53ef\u80fd\u4f1a\u5bfc\u81f4<strong>\u6027\u80fd\u74f6\u9888<\/strong>\uff0c\u4ee5\u4e0b\u4f18\u5316\u65b9\u6cd5\u53ef\u4ee5\u63d0\u5347\u6548\u7387\uff1a<\/p>\n\n\n\n<p>\u2705 <strong>1. \u907f\u514d\u56de\u6eaf\uff08Backtracking\uff09<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># \u4f4e\u6548\u5339\u914d\uff08\u56de\u6eaf\u8f83\u591a\uff09\npattern = r\"(a+)+b\"\n\n# \u9ad8\u6548\u5339\u914d\uff08\u4f7f\u7528\u975e\u8d2a\u5a6a\u6a21\u5f0f\uff09\npattern = r\"a+b\"<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u2705 <strong>2. \u4f7f\u7528 <code>re.compile()<\/code> \u9884\u7f16\u8bd1<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pattern = re.compile(r\"\\d{10}\")\nfor _ in range(1000):\n    pattern.search(\"1234567890\")  # \u9884\u7f16\u8bd1\u540e\u6267\u884c\u66f4\u5feb<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u2705 <strong>3. \u9009\u62e9\u5408\u9002\u7684\u5339\u914d\u65b9\u5f0f<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>re.match()<\/code> \u4ec5\u5339\u914d\u5f00\u5934\uff0c\u9002\u5408<strong>\u786e\u5b9a<\/strong>\u683c\u5f0f\u7684\u5b57\u7b26\u4e32<\/li>\n\n\n\n<li><code>re.search()<\/code> \u5728\u6574\u4e2a\u5b57\u7b26\u4e32\u4e2d\u67e5\u627e\u7b2c\u4e00\u4e2a\u5339\u914d\u9879<\/li>\n\n\n\n<li><code>re.findall()<\/code> \u67e5\u627e<strong>\u6240\u6709<\/strong>\u5339\u914d\u9879\uff0c\u9002\u5408\u6279\u91cf\u63d0\u53d6<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>17. \u7ec3\u4e60\u9898<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>17.1 \u63d0\u53d6\u65e5\u671f<\/strong><\/h3>\n\n\n\n<p><strong>\u76ee\u6807\u6587\u672c<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u4eca\u5929\u662f 2025-03-15, \u660e\u5929\u662f 2025\/03\/16, \u8fd8\u6709 03-17-2025.<\/code><\/pre>\n\n\n\n<p><strong>\u76ee\u6807<\/strong><br>\u63d0\u53d6\u6240\u6709<strong>\u65e5\u671f\u683c\u5f0f<\/strong>\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>YYYY-MM-DD<\/code><\/li>\n\n\n\n<li><code>YYYY\/MM\/DD<\/code><\/li>\n\n\n\n<li><code>MM-DD-YYYY<\/code><\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>pattern = r\"\\b\\d{4}&#91;-\/]\\d{2}&#91;-\/]\\d{2}\\b|\\b\\d{2}-\\d{2}-\\d{4}\\b\"\ntext = \"\u4eca\u5929\u662f 2025-03-15, \u660e\u5929\u662f 2025\/03\/16, \u8fd8\u6709 03-17-2025.\"\n\nmatches = re.findall(pattern, text)\nprint(matches)  # \u8f93\u51fa\uff1a&#91;'2025-03-15', '2025\/03\/16', '03-17-2025']<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python \u7684 re \u6a21\u5757\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u529f\u80fd\uff0c\u9002\u7528\u4e8e\u5b57\u7b26\u4e32\u5339\u914d\u3001\u641c\u7d22\u3001\u66ff\u6362\u7b49\u64cd\u4f5c\u3002\u4e0b\u9762\u662f Pytho [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2800,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[79],"tags":[],"class_list":["post-2799","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-3-"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2799","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=2799"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2799\/revisions"}],"predecessor-version":[{"id":2801,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2799\/revisions\/2801"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/2800"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=2799"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=2799"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=2799"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}