{"id":942535,"date":"2024-12-26T22:08:42","date_gmt":"2024-12-26T14:08:42","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/942535.html"},"modified":"2024-12-26T22:08:44","modified_gmt":"2024-12-26T14:08:44","slug":"python%e5%a6%82%e4%bd%95%e7%9c%8b%e5%8c%b9%e9%85%8d","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/942535.html","title":{"rendered":"python\u5982\u4f55\u770b\u5339\u914d"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25080316\/0c727075-4ccc-4674-abb9-0a0540ec9a1e.webp\" alt=\"python\u5982\u4f55\u770b\u5339\u914d\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u67e5\u770b\u5339\u914d\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\uff08regular expressions\uff09\u8fdb\u884c\u5b57\u7b26\u4e32\u5339\u914d\u3001\u5229\u7528\u5185\u7f6e\u7684\u5b57\u7b26\u4e32\u65b9\u6cd5\u5982find()\u548cin\u8fd0\u7b97\u7b26\u3001\u4f7f\u7528\u5916\u90e8\u5e93\u5982<code>re<\/code>\u6a21\u5757\u6765\u8fdb\u884c\u66f4\u590d\u6742\u7684\u6a21\u5f0f\u5339\u914d\u3002\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u5904\u7406\u5b57\u7b26\u4e32\u5339\u914d\u7684\u5f3a\u5927\u5de5\u5177\uff0c\u80fd\u591f\u5339\u914d\u590d\u6742\u7684\u6a21\u5f0f\u3001\u63d0\u53d6\u7279\u5b9a\u7684\u5b50\u4e32\u3001\u8fdb\u884c\u66ff\u6362\u64cd\u4f5c\u7b49\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u4f7f\u7528\u662f\u901a\u8fc7<code>re<\/code>\u6a21\u5757\u6765\u5b9e\u73b0\u7684\u3002<code>re<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u529f\u80fd\u5f3a\u5927\u7684\u65b9\u6cd5\u6765\u5904\u7406\u5b57\u7b26\u4e32\u5339\u914d\uff0c\u6bd4\u5982<code>re.match()<\/code>\u3001<code>re.search()<\/code>\u3001<code>re.findall()<\/code>\u7b49\u3002\u4e0b\u9762\u6211\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u53ca\u5176\u5e94\u7528\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u57fa\u7840<\/h2>\n<\/p>\n<p><h3>1\u3001\u4ec0\u4e48\u662f\u6b63\u5219\u8868\u8fbe\u5f0f<\/h3>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\uff08Regular Expression\uff0c\u7b80\u79f0regex\uff09\u662f\u4e00\u79cd\u7528\u6765\u63cf\u8ff0\u6216\u5339\u914d\u4e00\u7cfb\u5217\u7b26\u5408\u67d0\u4e2a\u53e5\u6cd5\u89c4\u5219\u7684\u5b57\u7b26\u4e32\u7684\u6a21\u5f0f\u3002\u5b83\u5728\u6587\u672c\u5904\u7406\u4e2d\u88ab\u5e7f\u6cdb\u5e94\u7528\uff0c\u5c24\u5176\u662f\u5728\u67e5\u627e\u3001\u66ff\u6362\u548c\u63d0\u53d6\u5b57\u7b26\u4e32\u7684\u573a\u666f\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>2\u3001Python\u4e2d\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u57fa\u7840<\/h3>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u5185\u7f6e\u7684<code>re<\/code>\u6a21\u5757\u6765\u652f\u6301\u6b63\u5219\u8868\u8fbe\u5f0f\u3002\u8981\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u9996\u5148\u9700\u8981\u5bfc\u5165\u8fd9\u4e2a\u6a21\u5757\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e8c\u3001\u5e38\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u65b9\u6cd5<\/h2>\n<\/p>\n<p><h3>1\u3001re.match()<\/h3>\n<\/p>\n<p><p><code>re.match()<\/code>\u65b9\u6cd5\u7528\u4e8e\u4ece\u5b57\u7b26\u4e32\u7684\u8d77\u59cb\u4f4d\u7f6e\u8fdb\u884c\u5339\u914d\u3002\u5982\u679c\u5728\u8d77\u59cb\u4f4d\u7f6e\u5339\u914d\u5230\u6a21\u5f0f\uff0c\u5219\u8fd4\u56de\u4e00\u4e2a\u5339\u914d\u5bf9\u8c61\uff1b\u5426\u5219\u8fd4\u56deNone\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>pattern = r&#39;\\d+&#39;  # \u5339\u914d\u4e00\u4e2a\u6216\u591a\u4e2a\u6570\u5b57<\/p>\n<p>string = &quot;123abc456&quot;<\/p>\n<p>match = re.match(pattern, string)<\/p>\n<p>if match:<\/p>\n<p>    print(&quot;Match found:&quot;, match.group())<\/p>\n<p>else:<\/p>\n<p>    print(&quot;No match&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001re.search()<\/h3>\n<\/p>\n<p><p><code>re.search()<\/code>\u65b9\u6cd5\u7528\u4e8e\u5728\u6574\u4e2a\u5b57\u7b26\u4e32\u4e2d\u641c\u7d22\u6a21\u5f0f\u3002\u4e0e<code>re.match()<\/code>\u4e0d\u540c\uff0c\u5b83\u4e0d\u8981\u6c42\u4ece\u8d77\u59cb\u4f4d\u7f6e\u5f00\u59cb\u5339\u914d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>pattern = r&#39;\\d+&#39;<\/p>\n<p>string = &quot;abc123&quot;<\/p>\n<p>search = re.search(pattern, string)<\/p>\n<p>if search:<\/p>\n<p>    print(&quot;Search found:&quot;, search.group())<\/p>\n<p>else:<\/p>\n<p>    print(&quot;No match&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3\u3001re.findall()<\/h3>\n<\/p>\n<p><p><code>re.findall()<\/code>\u65b9\u6cd5\u7528\u4e8e\u67e5\u627e\u5b57\u7b26\u4e32\u4e2d\u6240\u6709\u5339\u914d\u7684\u5b50\u4e32\uff0c\u5e76\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&#39;\\d+&#39;<\/p>\n<p>string = &quot;abc123def456&quot;<\/p>\n<p>matches = re.findall(pattern, string)<\/p>\n<p>print(&quot;Findall matches:&quot;, matches)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>4\u3001re.finditer()<\/h3>\n<\/p>\n<p><p><code>re.finditer()<\/code>\u65b9\u6cd5\u4e0e<code>re.findall()<\/code>\u7c7b\u4f3c\uff0c\u4f46\u8fd4\u56de\u7684\u662f\u4e00\u4e2a\u8fed\u4ee3\u5668\uff0c\u80fd\u591f\u9010\u4e2a\u8bbf\u95ee\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>string = &quot;abc123def456&quot;<\/p>\n<p>matches = re.finditer(pattern, string)<\/p>\n<p>for match in matches:<\/p>\n<p>    print(&quot;Finditer match:&quot;, match.group())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e09\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u8fdb\u9636<\/h2>\n<\/p>\n<p><h3>1\u3001\u6355\u83b7\u7ec4<\/h3>\n<\/p>\n<p><p>\u6355\u83b7\u7ec4\u662f\u901a\u8fc7\u5706\u62ec\u53f7<code>()<\/code>\u6765\u5b9a\u4e49\u7684\uff0c\u7528\u4e8e\u63d0\u53d6\u5b50\u6a21\u5f0f\u5339\u914d\u7684\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>pattern = r&#39;(\\d+)-(\\d+)&#39;<\/p>\n<p>string = &quot;Phone: 123-456&quot;<\/p>\n<p>match = re.search(pattern, string)<\/p>\n<p>if match:<\/p>\n<p>    print(&quot;Area code:&quot;, match.group(1))<\/p>\n<p>    print(&quot;Local number:&quot;, match.group(2))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001\u975e\u6355\u83b7\u7ec4<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u4e0d\u9700\u8981\u6355\u83b7\u7ec4\u4e2d\u7684\u5185\u5bb9\uff0c\u53ef\u4ee5\u4f7f\u7528<code>(?:...)<\/code>\u6765\u5b9a\u4e49\u975e\u6355\u83b7\u7ec4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>pattern = r&#39;(?:\\d+)-(\\d+)&#39;<\/p>\n<p>string = &quot;Phone: 123-456&quot;<\/p>\n<p>match = re.search(pattern, string)<\/p>\n<p>if match:<\/p>\n<p>    print(&quot;Local number:&quot;, match.group(1))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3\u3001\u65ad\u8a00<\/h3>\n<\/p>\n<p><p>\u65ad\u8a00\u7528\u4e8e\u6307\u5b9a\u5339\u914d\u7684\u4f4d\u7f6e\u6761\u4ef6\uff0c\u5305\u62ec\u524d\u77bb\u65ad\u8a00\uff08lookahead\uff09\u548c\u540e\u77bb\u65ad\u8a00\uff08lookbehind\uff09\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<h2><strong>\u524d\u77bb\u65ad\u8a00<\/strong><\/h2>\n<p>pattern = r&#39;\\d+(?= dollars)&#39;  # \u5339\u914d\u540e\u9762\u8ddf\u7740&quot;dollars&quot;\u7684\u6570\u5b57<\/p>\n<p>string = &quot;Price: 100 dollars&quot;<\/p>\n<p>match = re.search(pattern, string)<\/p>\n<p>if match:<\/p>\n<p>    print(&quot;Lookahead match:&quot;, match.group())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u56db\u3001\u5b57\u7b26\u4e32\u65b9\u6cd5\u5339\u914d<\/h2>\n<\/p>\n<p><h3>1\u3001find()<\/h3>\n<\/p>\n<p><p><code>find()<\/code>\u65b9\u6cd5\u7528\u4e8e\u67e5\u627e\u5b50\u4e32\u5728\u5b57\u7b26\u4e32\u4e2d\u7684\u4f4d\u7f6e\uff0c\u8fd4\u56de\u7b2c\u4e00\u4e2a\u5339\u914d\u7684\u7d22\u5f15\uff0c\u5982\u679c\u672a\u627e\u5230\u5219\u8fd4\u56de-1\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">string = &quot;Hello, world!&quot;<\/p>\n<p>substring = &quot;world&quot;<\/p>\n<p>index = string.find(substring)<\/p>\n<p>if index != -1:<\/p>\n<p>    print(&quot;Substring found at index:&quot;, index)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;Substring not found&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001in\u8fd0\u7b97\u7b26<\/h3>\n<\/p>\n<p><p><code>in<\/code>\u8fd0\u7b97\u7b26\u7528\u4e8e\u68c0\u67e5\u5b50\u4e32\u662f\u5426\u5b58\u5728\u4e8e\u5b57\u7b26\u4e32\u4e2d\uff0c\u8fd4\u56de\u5e03\u5c14\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">string = &quot;Hello, world!&quot;<\/p>\n<p>substring = &quot;world&quot;<\/p>\n<p>if substring in string:<\/p>\n<p>    print(&quot;Substring is in string&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;Substring is not in string&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e94\u3001\u603b\u7ed3<\/h2>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u67e5\u770b\u5b57\u7b26\u4e32\u5339\u914d\uff0c\u5176\u4e2d\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u6700\u5f3a\u5927\u548c\u7075\u6d3b\u7684\u5de5\u5177\u3002\u901a\u8fc7<code>re<\/code>\u6a21\u5757\u4e2d\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u8fdb\u884c\u4ece\u7b80\u5355\u5230\u590d\u6742\u7684\u5404\u79cd\u5339\u914d\u64cd\u4f5c\u3002\u540c\u65f6\uff0cPython\u7684\u5b57\u7b26\u4e32\u5185\u7f6e\u65b9\u6cd5\u5982<code>find()<\/code>\u548c<code>in<\/code>\u8fd0\u7b97\u7b26\u63d0\u4f9b\u4e86\u7b80\u5355\u76f4\u63a5\u7684\u5339\u914d\u65b9\u5f0f\uff0c\u9002\u5408\u4e8e\u4e0d\u9700\u8981\u590d\u6742\u6a21\u5f0f\u7684\u573a\u666f\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5339\u914d\u65b9\u6cd5\uff0c\u80fd\u591f\u63d0\u5347\u4ee3\u7801\u7684\u6548\u7387\u548c\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u68c0\u67e5\u5b57\u7b26\u4e32\u662f\u5426\u5339\u914d\u7279\u5b9a\u6a21\u5f0f\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>re<\/code>\u6a21\u5757\u6765\u68c0\u67e5\u5b57\u7b26\u4e32\u662f\u5426\u5339\u914d\u7279\u5b9a\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f\u3002\u9996\u5148\uff0c\u5bfc\u5165<code>re<\/code>\u6a21\u5757\uff0c\u5e76\u4f7f\u7528<code>re.match()<\/code>\u6216<code>re.search()<\/code>\u51fd\u6570\u6765\u8fdb\u884c\u5339\u914d\u3002<code>re.match()<\/code>\u4f1a\u4ece\u5b57\u7b26\u4e32\u7684\u8d77\u59cb\u4f4d\u7f6e\u68c0\u67e5\u5339\u914d\uff0c\u800c<code>re.search()<\/code>\u5219\u4f1a\u641c\u7d22\u6574\u4e2a\u5b57\u7b26\u4e32\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u57fa\u672c\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">import re\n\npattern = r&#39;\\d+&#39;  # \u5339\u914d\u4e00\u4e2a\u6216\u591a\u4e2a\u6570\u5b57\nstring = &quot;There are 123 apples&quot;\n\nif re.search(pattern, string):\n    print(&quot;\u5339\u914d\u6210\u529f\uff01&quot;)\nelse:\n    print(&quot;\u6ca1\u6709\u5339\u914d\u3002&quot;)\n<\/code><\/pre>\n<p><strong>Python\u4e2d\u5982\u4f55\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u63d0\u53d6\u5339\u914d\u7684\u5185\u5bb9\uff1f<\/strong><br \/>\u4f7f\u7528<code>re<\/code>\u6a21\u5757\u7684<code>re.findall()<\/code>\u51fd\u6570\u53ef\u4ee5\u63d0\u53d6\u6240\u6709\u5339\u914d\u7684\u5185\u5bb9\u3002\u8fd9\u4e2a\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u5217\u8868\uff0c\u5305\u542b\u6240\u6709\u7b26\u5408\u6a21\u5f0f\u7684\u5b50\u5b57\u7b26\u4e32\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">import re\n\npattern = r&#39;\\d+&#39;\nstring = &quot;There are 123 apples and 456 oranges&quot;\nmatches = re.findall(pattern, string)\n\nprint(&quot;\u627e\u5230\u7684\u6570\u5b57:&quot;, matches)  # \u8f93\u51fa: \u627e\u5230\u7684\u6570\u5b57: [&#39;123&#39;, &#39;456&#39;]\n<\/code><\/pre>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u4f18\u5316\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u5339\u914d\u6027\u80fd\uff1f<\/strong><br \/>\u4f18\u5316\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u5339\u914d\u6027\u80fd\u53ef\u4ee5\u901a\u8fc7\u51e0\u4e2a\u65b9\u9762\u6765\u5b9e\u73b0\u3002\u4f7f\u7528\u539f\u59cb\u5b57\u7b26\u4e32\uff08\u5728\u5b57\u7b26\u4e32\u524d\u52a0<code>r<\/code>\uff09\u53ef\u4ee5\u907f\u514d\u8f6c\u4e49\u5b57\u7b26\u7684\u95ee\u9898\uff0c\u4ece\u800c\u63d0\u9ad8\u53ef\u8bfb\u6027\u548c\u6027\u80fd\u3002\u6b64\u5916\uff0c\u907f\u514d\u8fc7\u4e8e\u590d\u6742\u7684\u8868\u8fbe\u5f0f\uff0c\u4f7f\u7528\u975e\u6355\u83b7\u7ec4\uff08<code>(?:...)<\/code>\uff09\u4ee5\u53ca\u9650\u5236\u5339\u914d\u6b21\u6570\uff08\u4f7f\u7528\u91cf\u8bcd\uff09\u90fd\u53ef\u4ee5\u63d0\u9ad8\u5339\u914d\u6548\u7387\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">import re\n\npattern = r&#39;(?:\\d{3})-\\d{2}-\\d{4}&#39;  # \u5339\u914d\u7c7b\u4f3c\u4e8e 123-45-6789 \u7684\u683c\u5f0f\nstring = &quot;My number is 123-45-6789&quot;\n\nif re.search(pattern, string):\n    print(&quot;\u5339\u914d\u6210\u529f\uff01&quot;)\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6709\u6548\u63d0\u5347\u6b63\u5219\u8868\u8fbe\u5f0f\u5728Python\u4e2d\u7684\u5339\u914d\u6027\u80fd\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u67e5\u770b\u5339\u914d\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\uff08regular expressions\uff09\u8fdb\u884c\u5b57\u7b26\u4e32\u5339\u914d\u3001\u5229 [&hellip;]","protected":false},"author":3,"featured_media":942540,"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\/942535"}],"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=942535"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/942535\/revisions"}],"predecessor-version":[{"id":942541,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/942535\/revisions\/942541"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/942540"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=942535"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=942535"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=942535"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}