{"id":959009,"date":"2024-12-27T03:36:57","date_gmt":"2024-12-26T19:36:57","guid":{"rendered":""},"modified":"2024-12-27T03:36:59","modified_gmt":"2024-12-26T19:36:59","slug":"python%e5%a6%82%e4%bd%95%e5%ae%9e%e7%8e%b0%e5%8c%b9%e9%85%8d%e6%93%8d%e4%bd%9c","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/959009.html","title":{"rendered":"python\u5982\u4f55\u5b9e\u73b0\u5339\u914d\u64cd\u4f5c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25101930\/358ac333-666b-4f69-805b-4ea874f64372.webp\" alt=\"python\u5982\u4f55\u5b9e\u73b0\u5339\u914d\u64cd\u4f5c\" \/><\/p>\n<p><p> <strong>Python\u5b9e\u73b0\u5339\u914d\u64cd\u4f5c\u7684\u65b9\u6cd5\u4e3b\u8981\u5305\u62ec\uff1a\u6b63\u5219\u8868\u8fbe\u5f0f\uff08regex\uff09\u3001\u5b57\u7b26\u4e32\u65b9\u6cd5\uff08\u5982find\u548cin\uff09\u3001\u96c6\u5408\u64cd\u4f5c\u3002<\/strong>\u5176\u4e2d\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u6700\u4e3a\u5f3a\u5927\u548c\u7075\u6d3b\u7684\u5de5\u5177\uff0c\u5b83\u53ef\u4ee5\u7528\u4e8e\u67e5\u627e\u3001\u5339\u914d\u548c\u64cd\u4f5c\u5b57\u7b26\u4e32\u4e2d\u7684\u590d\u6742\u6a21\u5f0f\u3002\u6b63\u5219\u8868\u8fbe\u5f0f\u901a\u8fc7\u6a21\u5757<code>re<\/code>\u63d0\u4f9b\u4e86\u4e00\u6574\u5957\u529f\u80fd\u4e30\u5bcc\u7684\u65b9\u6cd5\u6765\u5e2e\u52a9\u5b9e\u73b0\u5339\u914d\u64cd\u4f5c\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u63a2\u8ba8Python\u4e2d\u5b9e\u73b0\u5339\u914d\u64cd\u4f5c\u7684\u5404\u79cd\u65b9\u6cd5\u53ca\u5176\u5e94\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d<\/p>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\uff08regex\uff09\u662f\u4e00\u79cd\u5f3a\u5927\u4e14\u7075\u6d3b\u7684\u5de5\u5177\uff0c\u7528\u4e8e\u5904\u7406\u548c\u64cd\u4f5c\u5b57\u7b26\u4e32\u6a21\u5f0f\u3002\u5728Python\u4e2d\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f\u7531<code>re<\/code>\u6a21\u5757\u63d0\u4f9b\u652f\u6301\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5b9e\u73b0\u5339\u914d\u64cd\u4f5c\u7684\u8be6\u7ec6\u4ecb\u7ecd\u3002<\/p>\n<\/p>\n<p><h3>1. \u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u57fa\u672c\u6982\u5ff5<\/h3>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u4e00\u79cd\u7528\u4e8e\u5b9a\u4e49\u5b57\u7b26\u4e32\u6a21\u5f0f\u7684\u7279\u6b8a\u8bed\u6cd5\u3002\u5b83\u53ef\u4ee5\u7528\u4e8e\u641c\u7d22\u3001\u5339\u914d\u548c\u66ff\u6362\u5b57\u7b26\u4e32\u4e2d\u7684\u7279\u5b9a\u6a21\u5f0f\u3002\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u57fa\u672c\u5143\u7d20\u5305\u62ec\u5b57\u7b26\u96c6\u3001\u5143\u5b57\u7b26\u3001\u91cf\u8bcd\u548c\u5206\u7ec4\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u5b57\u7b26\u96c6<\/strong>\uff1a\u7528\u4e8e\u5339\u914d\u4e00\u7ec4\u5b57\u7b26\u3002\u4f8b\u5982\uff0c<code>[a-z]<\/code>\u8868\u793a\u5339\u914d\u6240\u6709\u5c0f\u5199\u5b57\u6bcd\u3002<\/li>\n<li><strong>\u5143\u5b57\u7b26<\/strong>\uff1a\u7528\u4e8e\u6784\u5efa\u590d\u6742\u6a21\u5f0f\u3002\u4f8b\u5982\uff0c<code>.<\/code>\u8868\u793a\u5339\u914d\u4efb\u610f\u5b57\u7b26\uff0c<code>^<\/code>\u8868\u793a\u5b57\u7b26\u4e32\u7684\u5f00\u5934\uff0c<code>$<\/code>\u8868\u793a\u5b57\u7b26\u4e32\u7684\u7ed3\u5c3e\u3002<\/li>\n<li><strong>\u91cf\u8bcd<\/strong>\uff1a\u7528\u4e8e\u6307\u5b9a\u5b57\u7b26\u51fa\u73b0\u7684\u6b21\u6570\u3002\u4f8b\u5982\uff0c<code>*<\/code>\u8868\u793a\u5339\u914d\u96f6\u6b21\u6216\u591a\u6b21\uff0c<code>+<\/code>\u8868\u793a\u5339\u914d\u4e00\u6b21\u6216\u591a\u6b21\uff0c<code>?<\/code>\u8868\u793a\u5339\u914d\u96f6\u6b21\u6216\u4e00\u6b21\u3002<\/li>\n<li><strong>\u5206\u7ec4<\/strong>\uff1a\u7528\u4e8e\u5c06\u591a\u4e2a\u5b57\u7b26\u7ec4\u5408\u5728\u4e00\u8d77\u4f5c\u4e3a\u4e00\u4e2a\u5355\u5143\u3002\u4f8b\u5982\uff0c<code>(abc)<\/code>\u8868\u793a\u5339\u914d\u5b57\u7b26\u4e32&quot;abc&quot;\u3002<\/li>\n<\/ul>\n<p><h3>2. \u4f7f\u7528<code>re<\/code>\u6a21\u5757\u8fdb\u884c\u5339\u914d<\/h3>\n<\/p>\n<p><p>Python\u7684<code>re<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u6267\u884c\u6b63\u5219\u8868\u8fbe\u5f0f\u64cd\u4f5c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u7684\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong><code>re.match(pattern, string)<\/code><\/strong>\uff1a\u4ece\u5b57\u7b26\u4e32\u7684\u8d77\u59cb\u4f4d\u7f6e\u5f00\u59cb\u5339\u914d\u6a21\u5f0f\u3002\u5982\u679c\u5339\u914d\u6210\u529f\uff0c\u5219\u8fd4\u56de\u4e00\u4e2a\u5339\u914d\u5bf9\u8c61\uff1b\u5426\u5219\uff0c\u8fd4\u56de<code>None<\/code>\u3002<\/li>\n<li><strong><code>re.search(pattern, string)<\/code><\/strong>\uff1a\u5728\u5b57\u7b26\u4e32\u4e2d\u641c\u7d22\u6a21\u5f0f\u3002\u5982\u679c\u627e\u5230\u5339\u914d\u9879\uff0c\u5219\u8fd4\u56de\u7b2c\u4e00\u4e2a\u5339\u914d\u5bf9\u8c61\uff1b\u5426\u5219\uff0c\u8fd4\u56de<code>None<\/code>\u3002<\/li>\n<li><strong><code>re.findall(pattern, string)<\/code><\/strong>\uff1a\u8fd4\u56de\u5b57\u7b26\u4e32\u4e2d\u6240\u6709\u4e0e\u6a21\u5f0f\u5339\u914d\u7684\u9879\uff0c\u4ee5\u5217\u8868\u5f62\u5f0f\u8fd4\u56de\u3002<\/li>\n<li><strong><code>re.finditer(pattern, string)<\/code><\/strong>\uff1a\u8fd4\u56de\u4e00\u4e2a\u8fed\u4ee3\u5668\uff0c\u751f\u6210\u5b57\u7b26\u4e32\u4e2d\u6240\u6709\u4e0e\u6a21\u5f0f\u5339\u914d\u7684\u9879\u3002<\/li>\n<li><strong><code>re.sub(pattern, repl, string)<\/code><\/strong>\uff1a\u4f7f\u7528\u6307\u5b9a\u7684\u66ff\u6362\u5b57\u7b26\u4e32\u66ff\u6362\u5b57\u7b26\u4e32\u4e2d\u6240\u6709\u4e0e\u6a21\u5f0f\u5339\u914d\u7684\u9879\u3002<\/li>\n<\/ul>\n<p><h3>3. \u6b63\u5219\u8868\u8fbe\u5f0f\u5e94\u7528\u793a\u4f8b<\/h3>\n<\/p>\n<p><p>\u5047\u8bbe\u6211\u4eec\u9700\u8981\u4ece\u4e00\u4e2a\u6587\u672c\u4e2d\u63d0\u53d6\u6240\u6709\u7684\u7535\u5b50\u90ae\u4ef6\u5730\u5740\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u6b63\u5219\u8868\u8fbe\u5f0f\u548c<code>re<\/code>\u6a21\u5757\u7684\u65b9\u6cd5\u6765\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>text = &quot;Please contact us at support@example.com or sales@example.com for further information.&quot;<\/p>\n<h2><strong>\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f\uff0c\u7528\u4e8e\u5339\u914d\u7535\u5b50\u90ae\u4ef6\u5730\u5740<\/strong><\/h2>\n<p>em<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>l_pattern = r&#39;[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}&#39;<\/p>\n<h2><strong>\u4f7f\u7528re.findall()\u63d0\u53d6\u6240\u6709\u7535\u5b50\u90ae\u4ef6\u5730\u5740<\/strong><\/h2>\n<p>emails = re.findall(email_pattern, text)<\/p>\n<p>print(emails)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u6b64\u793a\u4f8b\u4e2d\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f<code>[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}<\/code>\u7528\u4e8e\u5339\u914d\u7535\u5b50\u90ae\u4ef6\u5730\u5740\uff0c<code>re.findall()<\/code>\u51fd\u6570\u7528\u4e8e\u63d0\u53d6\u6587\u672c\u4e2d\u7684\u6240\u6709\u5339\u914d\u9879\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u5b57\u7b26\u4e32\u65b9\u6cd5\u5339\u914d<\/p>\n<\/p>\n<p><p>\u9664\u4e86\u6b63\u5219\u8868\u8fbe\u5f0f\uff0cPython\u8fd8\u63d0\u4f9b\u4e86\u4e00\u4e9b\u5185\u7f6e\u7684\u5b57\u7b26\u4e32\u65b9\u6cd5\u6765\u6267\u884c\u7b80\u5355\u7684\u5339\u914d\u64cd\u4f5c\u3002\u8fd9\u4e9b\u65b9\u6cd5\u901a\u5e38\u7528\u4e8e\u5904\u7406\u8f83\u4e3a\u7b80\u5355\u7684\u5339\u914d\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><h3>1. \u4f7f\u7528<code>in<\/code>\u64cd\u4f5c\u7b26<\/h3>\n<\/p>\n<p><p><code>in<\/code>\u64cd\u4f5c\u7b26\u7528\u4e8e\u68c0\u67e5\u4e00\u4e2a\u5b57\u7b26\u4e32\u662f\u5426\u5305\u542b\u5728\u53e6\u4e00\u4e2a\u5b57\u7b26\u4e32\u4e2d\u3002\u5b83\u662f\u8fdb\u884c\u7b80\u5355\u5339\u914d\u7684\u5feb\u6377\u65b9\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;Python is a popular programming language.&quot;<\/p>\n<h2><strong>\u68c0\u67e5\u5b57\u7b26\u4e32\u4e2d\u662f\u5426\u5305\u542b&quot;popular&quot;<\/strong><\/h2>\n<p>if &quot;popular&quot; in text:<\/p>\n<p>    print(&quot;Found &#39;popular&#39; in the text.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u4f7f\u7528<code>str.find()<\/code>\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p><code>str.find()<\/code>\u65b9\u6cd5\u8fd4\u56de\u5b50\u5b57\u7b26\u4e32\u5728\u5b57\u7b26\u4e32\u4e2d\u7684\u6700\u4f4e\u7d22\u5f15\u4f4d\u7f6e\u3002\u5982\u679c\u672a\u627e\u5230\u5b50\u5b57\u7b26\u4e32\uff0c\u5219\u8fd4\u56de<code>-1<\/code>\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;Python is a popular programming language.&quot;<\/p>\n<h2><strong>\u67e5\u627e\u5b50\u5b57\u7b26\u4e32&quot;popular&quot;\u7684\u4f4d\u7f6e<\/strong><\/h2>\n<p>index = text.find(&quot;popular&quot;)<\/p>\n<p>if index != -1:<\/p>\n<p>    print(f&quot;&#39;popular&#39; found at index {index}.&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;&#39;popular&#39; not found.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3. \u4f7f\u7528<code>str.startswith()<\/code>\u548c<code>str.endswith()<\/code>\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p><code>str.startswith()<\/code>\u548c<code>str.endswith()<\/code>\u65b9\u6cd5\u7528\u4e8e\u68c0\u67e5\u5b57\u7b26\u4e32\u662f\u5426\u4ee5\u7279\u5b9a\u524d\u7f00\u6216\u540e\u7f00\u5f00\u5934\u6216\u7ed3\u5c3e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">filename = &quot;report.pdf&quot;<\/p>\n<h2><strong>\u68c0\u67e5\u6587\u4ef6\u540d\u662f\u5426\u4ee5&quot;report&quot;\u5f00\u5934<\/strong><\/h2>\n<p>if filename.startswith(&quot;report&quot;):<\/p>\n<p>    print(&quot;The filename starts with &#39;report&#39;.&quot;)<\/p>\n<h2><strong>\u68c0\u67e5\u6587\u4ef6\u540d\u662f\u5426\u4ee5&quot;.pdf&quot;\u7ed3\u5c3e<\/strong><\/h2>\n<p>if filename.endswith(&quot;.pdf&quot;):<\/p>\n<p>    print(&quot;The filename is a PDF file.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u96c6\u5408\u64cd\u4f5c\u5339\u914d<\/p>\n<\/p>\n<p><p>\u96c6\u5408\u662f\u4e00\u79cd\u65e0\u5e8f\u4e14\u4e0d\u91cd\u590d\u7684\u5143\u7d20\u96c6\u5408\u3002\u901a\u8fc7\u4f7f\u7528\u96c6\u5408\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u8fdb\u884c\u96c6\u5408\u4e4b\u95f4\u7684\u5339\u914d\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h3>1. \u4f7f\u7528\u96c6\u5408\u6c42\u4ea4\u96c6<\/h3>\n<\/p>\n<p><p>\u96c6\u5408\u7684\u4ea4\u96c6\u64cd\u4f5c\u53ef\u4ee5\u7528\u4e8e\u67e5\u627e\u4e24\u4e2a\u96c6\u5408\u4e2d\u7684\u5171\u540c\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">set1 = {&quot;apple&quot;, &quot;banana&quot;, &quot;cherry&quot;}<\/p>\n<p>set2 = {&quot;banana&quot;, &quot;cherry&quot;, &quot;date&quot;}<\/p>\n<h2><strong>\u6c42\u96c6\u5408\u7684\u4ea4\u96c6<\/strong><\/h2>\n<p>intersection = set1 &amp; set2<\/p>\n<p>print(intersection)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u4f7f\u7528\u96c6\u5408\u6c42\u5e76\u96c6<\/h3>\n<\/p>\n<p><p>\u96c6\u5408\u7684\u5e76\u96c6\u64cd\u4f5c\u53ef\u4ee5\u7528\u4e8e\u5408\u5e76\u4e24\u4e2a\u96c6\u5408\u4e2d\u7684\u6240\u6709\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">set1 = {&quot;apple&quot;, &quot;banana&quot;, &quot;cherry&quot;}<\/p>\n<p>set2 = {&quot;banana&quot;, &quot;cherry&quot;, &quot;date&quot;}<\/p>\n<h2><strong>\u6c42\u96c6\u5408\u7684\u5e76\u96c6<\/strong><\/h2>\n<p>union = set1 | set2<\/p>\n<p>print(union)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3. \u4f7f\u7528\u96c6\u5408\u6c42\u5dee\u96c6<\/h3>\n<\/p>\n<p><p>\u96c6\u5408\u7684\u5dee\u96c6\u64cd\u4f5c\u7528\u4e8e\u67e5\u627e\u4e00\u4e2a\u96c6\u5408\u4e2d\u4e0d\u5c5e\u4e8e\u53e6\u4e00\u4e2a\u96c6\u5408\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">set1 = {&quot;apple&quot;, &quot;banana&quot;, &quot;cherry&quot;}<\/p>\n<p>set2 = {&quot;banana&quot;, &quot;cherry&quot;, &quot;date&quot;}<\/p>\n<h2><strong>\u6c42\u96c6\u5408\u7684\u5dee\u96c6<\/strong><\/h2>\n<p>difference = set1 - set2<\/p>\n<p>print(difference)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u5176\u4ed6\u5339\u914d\u65b9\u6cd5<\/p>\n<\/p>\n<p><p>\u9664\u4e86\u4e0a\u8ff0\u65b9\u6cd5\u5916\uff0c\u8fd8\u6709\u5176\u4ed6\u4e00\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u7528\u4e8e\u5b9e\u73b0\u5339\u914d\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h3>1. \u4f7f\u7528<code>str.translate()<\/code>\u548c<code>str.maketrans()<\/code><\/h3>\n<\/p>\n<p><p><code>str.translate()<\/code>\u65b9\u6cd5\u4e0e<code>str.maketrans()<\/code>\u65b9\u6cd5\u7ed3\u5408\u4f7f\u7528\uff0c\u53ef\u4ee5\u5b9e\u73b0\u5b57\u7b26\u66ff\u6362\u548c\u5220\u9664\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;Hello, World!&quot;<\/p>\n<h2><strong>\u521b\u5efa\u7ffb\u8bd1\u8868\uff0c\u5c06&quot;l&quot;\u66ff\u6362\u4e3a&quot;x&quot;\u5e76\u5220\u9664&quot;o&quot;<\/strong><\/h2>\n<p>translation_table = str.maketrans(&quot;lo&quot;, &quot;x&quot;, &quot;o&quot;)<\/p>\n<h2><strong>\u5e94\u7528\u7ffb\u8bd1\u8868<\/strong><\/h2>\n<p>translated_text = text.translate(translation_table)<\/p>\n<p>print(translated_text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u4f7f\u7528<code>fnmatch<\/code>\u6a21\u5757<\/h3>\n<\/p>\n<p><p><code>fnmatch<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u79cd\u7b80\u5355\u7684\u65b9\u6cd5\u6765\u5339\u914d\u6587\u4ef6\u540d\u6a21\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import fnmatch<\/p>\n<p>filename = &quot;example.txt&quot;<\/p>\n<h2><strong>\u4f7f\u7528\u901a\u914d\u7b26\u6a21\u5f0f\u5339\u914d\u6587\u4ef6\u540d<\/strong><\/h2>\n<p>if fnmatch.fnmatch(filename, &quot;*.txt&quot;):<\/p>\n<p>    print(&quot;The filename matches the pattern.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001\u5339\u914d\u64cd\u4f5c\u7684\u5e94\u7528\u573a\u666f<\/p>\n<\/p>\n<p><p>\u5339\u914d\u64cd\u4f5c\u5728\u8bb8\u591a\u5e94\u7528\u573a\u666f\u4e2d\u90fd\u975e\u5e38\u6709\u7528\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u5e94\u7528\u573a\u666f\uff1a<\/p>\n<\/p>\n<p><h3>1. \u6570\u636e\u6e05\u7406\u548c\u9884\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5206\u6790\u548c<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u4e2d\uff0c\u6570\u636e\u6e05\u7406\u548c\u9884\u5904\u7406\u662f\u975e\u5e38\u91cd\u8981\u7684\u6b65\u9aa4\u3002\u5339\u914d\u64cd\u4f5c\u53ef\u4ee5\u7528\u4e8e\u8bc6\u522b\u5e76\u5904\u7406\u6570\u636e\u4e2d\u7684\u5f02\u5e38\u503c\u3001\u7f3a\u5931\u503c\u548c\u91cd\u590d\u503c\u3002<\/p>\n<\/p>\n<p><h3>2. \u6587\u672c\u5206\u6790\u548c\u81ea\u7136\u8bed\u8a00\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u5728\u6587\u672c\u5206\u6790\u548c\u81ea\u7136\u8bed\u8a00\u5904\u7406\uff08NLP\uff09\u4e2d\uff0c\u5339\u914d\u64cd\u4f5c\u7528\u4e8e\u6807\u8bb0\u3001\u63d0\u53d6\u548c\u5206\u6790\u6587\u672c\u4e2d\u7684\u6a21\u5f0f\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u8bc6\u522b\u6587\u672c\u4e2d\u7684\u7279\u5b9a\u5173\u952e\u8bcd\u3001\u77ed\u8bed\u6216\u5b9e\u4f53\u3002<\/p>\n<\/p>\n<p><h3>3. \u6587\u4ef6\u548c\u76ee\u5f55\u64cd\u4f5c<\/h3>\n<\/p>\n<p><p>\u5339\u914d\u64cd\u4f5c\u53ef\u4ee5\u7528\u4e8e\u67e5\u627e\u548c\u5904\u7406\u6587\u4ef6\u548c\u76ee\u5f55\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528\u6a21\u5f0f\u5339\u914d\u6765\u7b5b\u9009\u7279\u5b9a\u7c7b\u578b\u7684\u6587\u4ef6\u6216\u67e5\u627e\u7b26\u5408\u6761\u4ef6\u7684\u76ee\u5f55\u3002<\/p>\n<\/p>\n<p><h3>4. \u6570\u636e\u5e93\u67e5\u8be2<\/h3>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5e93\u67e5\u8be2\u4e2d\uff0c\u5339\u914d\u64cd\u4f5c\u7528\u4e8e\u7b5b\u9009\u548c\u63d0\u53d6\u7b26\u5408\u6761\u4ef6\u7684\u6570\u636e\u8bb0\u5f55\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528SQL\u4e2d\u7684<code>LIKE<\/code>\u5173\u952e\u5b57\u8fdb\u884c\u6a21\u5f0f\u5339\u914d\u67e5\u8be2\u3002<\/p>\n<\/p>\n<p><h3>5. \u5b89\u5168\u548c\u9690\u79c1<\/h3>\n<\/p>\n<p><p>\u5339\u914d\u64cd\u4f5c\u5728\u5b89\u5168\u548c\u9690\u79c1\u65b9\u9762\u4e5f\u6709\u5e94\u7528\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528\u5339\u914d\u64cd\u4f5c\u8bc6\u522b\u548c\u8fc7\u6ee4\u654f\u611f\u4fe1\u606f\uff0c\u9632\u6b62\u6570\u636e\u6cc4\u9732\u3002<\/p>\n<\/p>\n<p><p>\u603b\u7ed3\uff1aPython\u4e2d\u5b9e\u73b0\u5339\u914d\u64cd\u4f5c\u7684\u65b9\u6cd5\u591a\u79cd\u591a\u6837\uff0c\u5305\u62ec\u6b63\u5219\u8868\u8fbe\u5f0f\u3001\u5b57\u7b26\u4e32\u65b9\u6cd5\u3001\u96c6\u5408\u64cd\u4f5c\u7b49\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u72ec\u7279\u7684\u4f18\u52bf\u548c\u9002\u7528\u573a\u666f\u3002\u901a\u8fc7\u7075\u6d3b\u8fd0\u7528\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u9ad8\u6548\u5730\u5904\u7406\u548c\u64cd\u4f5c\u5b57\u7b26\u4e32\u6570\u636e\u3002\u5728\u9009\u62e9\u5339\u914d\u65b9\u6cd5\u65f6\uff0c\u5e94\u6839\u636e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u548c\u6570\u636e\u7279\u70b9\u8fdb\u884c\u5408\u7406\u9009\u62e9\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\u5339\u914d\u64cd\u4f5c\uff1f<\/strong><br \/>Python\u63d0\u4f9b\u4e86<code>re<\/code>\u6a21\u5757\uff0c\u7528\u4e8e\u5904\u7406\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u5339\u914d\u64cd\u4f5c\u3002\u901a\u8fc7\u4f7f\u7528<code>re.match()<\/code>, <code>re.search()<\/code>, \u548c <code>re.findall()<\/code>\u7b49\u51fd\u6570\uff0c\u4f60\u53ef\u4ee5\u5b9e\u73b0\u5b57\u7b26\u4e32\u7684\u6a21\u5f0f\u5339\u914d\u3002<code>re.match()<\/code>\u7528\u4e8e\u4ece\u5b57\u7b26\u4e32\u7684\u8d77\u59cb\u4f4d\u7f6e\u5339\u914d\u6a21\u5f0f\uff0c\u800c<code>re.search()<\/code>\u4f1a\u626b\u63cf\u6574\u4e2a\u5b57\u7b26\u4e32\u5bfb\u627e\u7b2c\u4e00\u4e2a\u5339\u914d\u7684\u6a21\u5f0f\uff0c<code>re.findall()<\/code>\u5219\u8fd4\u56de\u6240\u6709\u5339\u914d\u7684\u7ed3\u679c\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">import re\n\npattern = r&#39;\\d+&#39;  # \u5339\u914d\u6570\u5b57\nstring = &quot;\u57282023\u5e74\uff0c\u6211\u4eec\u5b66\u4e60Python\u3002&quot;\nmatches = re.findall(pattern, string)\nprint(matches)  # \u8f93\u51fa: [&#39;2023&#39;]\n<\/code><\/pre>\n<p><strong>Python\u4e2d\u5982\u4f55\u5904\u7406\u5927\u5c0f\u5199\u4e0d\u654f\u611f\u7684\u5339\u914d\uff1f<\/strong><br \/>\u4e3a\u4e86\u5728\u5339\u914d\u65f6\u5ffd\u7565\u5927\u5c0f\u5199\uff0c\u53ef\u4ee5\u4f7f\u7528<code>re.IGNORECASE<\/code>\u6807\u5fd7\u3002\u5728\u8c03\u7528\u5339\u914d\u51fd\u6570\u65f6\uff0c\u5c06\u8be5\u6807\u5fd7\u4f5c\u4e3a\u53c2\u6570\u4f20\u5165\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">import re\n\npattern = r&#39;python&#39;\nstring = &quot;Python \u662f\u4e00\u79cd\u7f16\u7a0b\u8bed\u8a00\u3002&quot;\nmatch = re.search(pattern, string, re.IGNORECASE)\nif match:\n    print(&quot;\u5339\u914d\u6210\u529f\uff01&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u4e2a\u793a\u4f8b\u5c06\u6210\u529f\u5339\u914d&quot;Python&quot;\uff0c\u5373\u4f7f\u5b83\u7684\u5927\u5c0f\u5199\u4e0d\u540c\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u63d0\u53d6\u7279\u5b9a\u683c\u5f0f\u7684\u5b57\u7b26\u4e32\uff1f<\/strong><br \/>\u5982\u679c\u4f60\u60f3\u63d0\u53d6\u7279\u5b9a\u683c\u5f0f\u7684\u5b57\u7b26\u4e32\uff0c\u6bd4\u5982\u7535\u5b50\u90ae\u4ef6\u5730\u5740\u6216\u65e5\u671f\uff0c\u53ef\u4ee5\u4f7f\u7528\u76f8\u5e94\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u63d0\u53d6\u7535\u5b50\u90ae\u4ef6\u5730\u5740\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">import re\n\ntext = &quot;\u8054\u7cfb\u6211\uff1aexample@example.com \u6216 support@example.org&quot;\nemails = re.findall(r&#39;[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,}&#39;, text)\nprint(emails)  # \u8f93\u51fa: [&#39;example@example.com&#39;, &#39;support@example.org&#39;]\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u5e2e\u52a9\u4f60\u5feb\u901f\u4ece\u6587\u672c\u4e2d\u83b7\u53d6\u6240\u9700\u7684\u4fe1\u606f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5b9e\u73b0\u5339\u914d\u64cd\u4f5c\u7684\u65b9\u6cd5\u4e3b\u8981\u5305\u62ec\uff1a\u6b63\u5219\u8868\u8fbe\u5f0f\uff08regex\uff09\u3001\u5b57\u7b26\u4e32\u65b9\u6cd5\uff08\u5982find\u548cin\uff09\u3001\u96c6\u5408\u64cd\u4f5c\u3002\u5176 [&hellip;]","protected":false},"author":3,"featured_media":959014,"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\/959009"}],"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=959009"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/959009\/revisions"}],"predecessor-version":[{"id":959018,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/959009\/revisions\/959018"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/959014"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=959009"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=959009"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=959009"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}