{"id":176894,"date":"2024-05-08T19:20:01","date_gmt":"2024-05-08T11:20:01","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/176894.html"},"modified":"2024-05-08T19:20:05","modified_gmt":"2024-05-08T11:20:05","slug":"python%e6%ad%a3%e5%88%99%e8%a1%a8%e8%be%be%e5%bc%8f%e6%80%8e%e4%b9%88%e5%ae%9e%e7%8e%b0%e5%8c%b9%e9%85%8d%e5%88%b0%e6%89%80%e6%9c%89%e7%ac%a6%e5%90%88%e9%a1%b9","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/176894.html","title":{"rendered":"python\u6b63\u5219\u8868\u8fbe\u5f0f\u600e\u4e48\u5b9e\u73b0\u5339\u914d\u5230\u6240\u6709\u7b26\u5408\u9879"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27060719\/03792be3-0cfa-4cd3-b72b-e599f9aff3bf.webp\" alt=\"python\u6b63\u5219\u8868\u8fbe\u5f0f\u600e\u4e48\u5b9e\u73b0\u5339\u914d\u5230\u6240\u6709\u7b26\u5408\u9879\" \/><\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u4e00\u79cd\u7528\u6765\u4ece\u5b57\u7b26\u4e32\u4e2d\u63d0\u53d6\u7b26\u5408\u67d0\u4e2a\u6a21\u5f0f\u7684\u6587\u672c\u7684\u5de5\u5177\u3002<strong>\u5728Python\u4e2d\uff0c\u8981\u5b9e\u73b0\u5339\u914d\u5230\u6240\u6709\u7b26\u5408\u9879\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>re<\/code>\u6a21\u5757\u4e2d\u7684<code>findall<\/code>\u51fd\u6570\u3001<code>finditer<\/code>\u51fd\u6570\u6216\u8005\u4f7f\u7528<code>re.compile<\/code>\u914d\u5408<code>findall<\/code>\u6216<code>finditer<\/code><\/strong>\u3002\u8fd9\u4e9b\u51fd\u6570\u53ef\u4ee5\u8fed\u4ee3\u5339\u914d\u5b57\u7b26\u4e32\u4e2d\u6240\u6709\u7b26\u5408\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u975e\u91cd\u53e0\u6a21\u5f0f\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u5217\u8868\u6216\u8fed\u4ee3\u5668\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4f7f\u7528<code>findall<\/code>\u51fd\u6570\uff0c\u5b83\u4f1a\u626b\u63cf\u6574\u4e2a\u5b57\u7b26\u4e32\uff0c\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u6240\u6709\u5339\u914d\u7684\u9879\u7684\u5217\u8868\u3002\u5982\u679c\u6b63\u5219\u8868\u8fbe\u5f0f\u4e2d\u5305\u542b\u4e86\u5206\u7ec4\uff08\u5373\u4f7f\u7528\u5c0f\u62ec\u53f7<code>()<\/code>\uff09\uff0c\u90a3\u4e48<code>findall<\/code>\u4f1a\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u6bcf\u4e2a\u5206\u7ec4\u5339\u914d\u5b57\u7b26\u4e32\u7684\u5143\u7ec4\u5217\u8868\u3002\u8fd9\u5bf9\u4e8e\u4ece\u6587\u672c\u4e2d\u63d0\u53d6\u7279\u5b9a\u7684\u4fe1\u606f\u7247\u6bb5\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528<code>findall<\/code>\u51fd\u6570<\/h3>\n<\/p>\n<p><p><strong>\u8981\u4f7f\u7528<code>findall<\/code>\u51fd\u6570\uff0c\u9996\u5148\u9700\u8981\u786e\u5b9a\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u6a21\u5f0f<\/strong>\u3002\u6bd4\u5982\u8981\u5339\u914d\u6240\u6709\u7684\u7535\u5b50\u90ae\u4ef6\u5730\u5740\uff0c\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f<code>r&#039;[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}&#039;<\/code>\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<h2><strong>\u793a\u4f8b\u6587\u672c<\/strong><\/h2>\n<p>text = &quot;Please contact us at contact@example.com, support@example.org or sales@example.net&quot;<\/p>\n<h2><strong>\u6b63\u5219\u8868\u8fbe\u5f0f\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>ls = re.findall(r&#039;[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}&#039;, text)<\/p>\n<h2><strong>\u8f93\u51fa\u6240\u6709\u5339\u914d\u7684\u7535\u5b50\u90ae\u4ef6\u5730\u5740<\/strong><\/h2>\n<p>print(emails)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>findall<\/code>\u51fd\u6570\u5c06\u8fd4\u56de\u4e00\u4e2a\u5217\u8868\uff0c\u5305\u542b\u6587\u672c\u4e2d\u627e\u5230\u7684\u6240\u6709\u7535\u5b50\u90ae\u4ef6\u5730\u5740\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528<code>finditer<\/code>\u51fd\u6570<\/h3>\n<\/p>\n<p><p><strong>\u5982\u679c\u9700\u8981\u66f4\u591a\u7684\u5339\u914d\u4fe1\u606f\uff0c\u4f8b\u5982\u5339\u914d\u7684\u4f4d\u7f6e\uff0c\u53ef\u4ee5\u4f7f\u7528<code>finditer<\/code>\u51fd\u6570<\/strong>\u3002<code>finditer<\/code>\u4f1a\u8fd4\u56de\u4e00\u4e2a\u8fed\u4ee3\u5668\uff0c\u800c\u4e0d\u662f\u5217\u8868\uff0c\u8fed\u4ee3\u5668\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u90fd\u662f\u4e00\u4e2a<code>Match<\/code>\u5bf9\u8c61\uff0c\u5305\u542b\u4e86\u5339\u914d\u7684\u8be6\u7ec6\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<h2><strong>\u793a\u4f8b\u6587\u672c<\/strong><\/h2>\n<p>text = &quot;Please contact us at contact@example.com, support@example.org or sales@example.net&quot;<\/p>\n<h2><strong>\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u7535\u5b50\u90ae\u4ef6\u5730\u5740<\/strong><\/h2>\n<p>matches = re.finditer(r&#039;[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}&#039;, text)<\/p>\n<h2><strong>\u8f93\u51fa\u6240\u6709\u5339\u914d\u7684\u7535\u5b50\u90ae\u4ef6\u5730\u5740\u53ca\u5176\u4f4d\u7f6e<\/strong><\/h2>\n<p>for match in matches:<\/p>\n<p>    print(match.group(), match.start(), match.end())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528<code>finditer<\/code>\u51fd\u6570\u6765\u4e0d\u4ec5\u83b7\u53d6\u5339\u914d\u7684\u5b57\u7b26\u4e32\uff0c\u800c\u4e14\u8fd8\u53ef\u4ee5\u786e\u5b9a\u5b83\u4eec\u5728\u539f\u59cb\u6587\u672c\u4e2d\u7684\u4f4d\u7f6e\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528<code>re.compile<\/code>\u4f18\u5316\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u4f7f\u7528<\/h3>\n<\/p>\n<p><p><strong>\u5f53\u9700\u8981\u591a\u6b21\u4f7f\u7528\u540c\u4e00\u4e2a\u6b63\u5219\u8868\u8fbe\u5f0f\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>re.compile<\/code>\u6765\u7f16\u8bd1\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u63d0\u9ad8\u5339\u914d\u6548\u7387<\/strong>\u3002\u4e00\u65e6\u7f16\u8bd1\u597d\uff0c\u5c31\u53ef\u4ee5\u4f7f\u7528\u7f16\u8bd1\u540e\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u5bf9\u8c61\u8fdb\u884c\u641c\u7d22\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<h2><strong>\u7f16\u8bd1\u6b63\u5219\u8868\u8fbe\u5f0f<\/strong><\/h2>\n<p>email_pattern = re.compile(r&#039;[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}&#039;)<\/p>\n<h2><strong>\u793a\u4f8b\u6587\u672c<\/strong><\/h2>\n<p>text = &quot;Please contact us at contact@example.com, support@example.org or sales@example.net&quot;<\/p>\n<h2><strong>\u4f7f\u7528\u7f16\u8bd1\u597d\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u8fdb\u884c\u5339\u914d<\/strong><\/h2>\n<p>emails = email_pattern.findall(text)<\/p>\n<h2><strong>\u8f93\u51fa\u6240\u6709\u5339\u914d\u7684\u7535\u5b50\u90ae\u4ef6\u5730\u5740<\/strong><\/h2>\n<p>print(emails)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u4f7f\u7528<code>re.compile<\/code>\u7f16\u8bd1\u4e86\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u7136\u540e\u901a\u8fc7\u7f16\u8bd1\u540e\u7684\u5bf9\u8c61\u4f7f\u7528<code>findall<\/code>\u65b9\u6cd5\u63d0\u53d6\u4e86\u6240\u6709\u7684\u7535\u5b50\u90ae\u4ef6\u5730\u5740\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5904\u7406\u6b63\u5219\u8868\u8fbe\u5f0f\u4e2d\u7684\u7279\u6b8a\u60c5\u51b5<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u5019\u8fdb\u884c\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u65f6\uff0c\u9700\u8981\u5904\u7406\u4e00\u4e9b\u7279\u6b8a\u7684\u60c5\u51b5\uff0c\u4f8b\u5982\u6b63\u5219\u8868\u8fbe\u5f0f\u4e2d\u7684\u7279\u6b8a\u5b57\u7b26\u3001\u8d2a\u5a6a\u4e0e\u975e\u8d2a\u5a6a\u5339\u914d\u3001\u591a\u884c\u5339\u914d\u7b49\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u7279\u6b8a\u5b57\u7b26<\/strong>\uff1a\u5728\u6b63\u5219\u8868\u8fbe\u5f0f\u4e2d\uff0c\u67d0\u4e9b\u5b57\u7b26\u62e5\u6709\u7279\u6b8a\u7684\u610f\u4e49\uff0c\u6bd4\u5982\u70b9\uff08<code>.<\/code>\uff09\u8868\u793a\u5339\u914d\u9664\u6362\u884c\u7b26\u4e4b\u5916\u7684\u4efb\u610f\u5355\u4e2a\u5b57\u7b26\uff0c\u661f\u53f7\uff08<code>*<\/code>\uff09\u8868\u793a\u5339\u914d\u524d\u9762\u7684\u8868\u8fbe\u5f0f\u96f6\u6b21\u6216\u591a\u6b21\u3002\u5982\u679c\u60f3\u8981\u5339\u914d\u8fd9\u4e9b\u7279\u6b8a\u5b57\u7b26\u672c\u8eab\uff0c\u9700\u8981\u4f7f\u7528\u53cd\u659c\u6760\uff08<code><\/code>\uff09\u8fdb\u884c\u8f6c\u4e49\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8d2a\u5a6a\u4e0e\u975e\u8d2a\u5a6a\u5339\u914d<\/strong>\uff1a\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u5339\u914d\u662f\u8d2a\u5a6a\u7684\uff0c\u4e5f\u5c31\u662f\u4f1a\u5c3d\u53ef\u80fd\u591a\u5730\u5339\u914d\u5b57\u7b26\u3002\u5728\u91cf\u8bcd\u540e\u6dfb\u52a0\u95ee\u53f7\uff08<code>?<\/code>\uff09\u53ef\u4ee5\u8f6c\u4e3a\u975e\u8d2a\u5a6a\u5339\u914d\uff0c\u5339\u914d\u5c3d\u53ef\u80fd\u5c11\u7684\u5b57\u7b26\u3002\u5728\u5904\u7406\u590d\u6742\u7684\u6587\u672c\u65f6\uff0c\u8fd9\u53ef\u4ee5\u5e2e\u52a9\u66f4\u7cbe\u786e\u5730\u5b9a\u4f4d\u6240\u9700\u7684\u6587\u672c\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u591a\u884c\u5339\u914d<\/strong>\uff1a\u6b63\u5219\u8868\u8fbe\u5f0f\u5728\u5904\u7406\u8de8\u591a\u884c\u7684\u6587\u672c\u65f6\uff0c\u9700\u8981\u4f7f\u7528\u5230\u7279\u5b9a\u7684\u6807\u5fd7\u3002\u4f8b\u5982\uff0c<code>re.MULTILINE<\/code>\u6807\u5fd7\u53ef\u4ee5\u5f71\u54cd^\u548c$\u7684\u884c\u4e3a\uff0c\u4f7f\u5b83\u4eec\u5206\u522b\u5339\u914d\u6bcf\u4e00\u884c\u7684\u5f00\u5934\u548c\u7ed3\u5c3e\uff0c\u800c\u4e0d\u4ec5\u4ec5\u662f\u6574\u4e2a\u5b57\u7b26\u4e32\u7684\u5f00\u5934\u548c\u7ed3\u5c3e\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u7ed3\u5408\u4f7f\u7528\u8fd9\u4e9b\u529f\u80fd\u548c\u65b9\u6cd5\uff0c\u53ef\u4ee5\u4f7fPython\u4e2d\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u66f4\u52a0\u5f3a\u5927\u548c\u7075\u6d3b\u3002\u9002\u5f53\u5730\u4f7f\u7528<code>findall<\/code>\u3001<code>finditer<\/code>\u4ee5\u53ca\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u7f16\u8bd1\u7248\u672c\u80fd\u591f\u786e\u4fdd\u9ad8\u6548\u7387\u5730\u5339\u914d\u5230\u6240\u6709\u7b26\u5408\u9879\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>Q: \u5982\u4f55\u4f7f\u7528Python\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u5230\u6240\u6709\u7b26\u5408\u9879\uff1f<\/strong><\/p>\n<p>A: Python\u63d0\u4f9b\u4e86re\u6a21\u5757\uff0c\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u8fdb\u884c\u5b57\u7b26\u4e32\u5339\u914d\u3002\u4ee5\u4e0b\u662f\u5b9e\u73b0\u5339\u914d\u5230\u6240\u6709\u7b26\u5408\u9879\u7684\u65b9\u6cd5\uff1a<\/p>\n<ol>\n<li>\n<p>\u4f7f\u7528re.finditer()\u51fd\u6570\uff1a\u53ef\u4ee5\u4f7f\u7528re.finditer(pattern, string)\u51fd\u6570\u627e\u5230\u6240\u6709\u5339\u914d\u7684\u9879\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u8fed\u4ee3\u5668\u3002\u901a\u8fc7\u904d\u5386\u8fed\u4ee3\u5668\uff0c\u53ef\u4ee5\u9010\u4e2a\u8bbf\u95ee\u5230\u6bcf\u4e2a\u5339\u914d\u7684\u9879\u3002<\/p>\n<\/li>\n<li>\n<p>\u4f7f\u7528re.findall()\u51fd\u6570\uff1a\u53ef\u4ee5\u4f7f\u7528re.findall(pattern, string)\u51fd\u6570\u4e00\u6b21\u6027\u5730\u627e\u5230\u6240\u6709\u5339\u914d\u7684\u9879\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u5217\u8868\u3002\u8be5\u5217\u8868\u5305\u542b\u4e86\u6240\u6709\u5339\u914d\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<\/li>\n<li>\n<p>\u7ed3\u5408\u4f7f\u7528re.compile()\u548cre.finditer()\uff1a\u53ef\u4ee5\u5148\u4f7f\u7528re.compile(pattern)\u5c06\u6b63\u5219\u8868\u8fbe\u5f0f\u7f16\u8bd1\u6210\u4e00\u4e2apattern\u5bf9\u8c61\uff0c\u7136\u540e\u518d\u4f7f\u7528\u8be5\u5bf9\u8c61\u7684finditer()\u65b9\u6cd5\u6765\u627e\u5230\u6240\u6709\u5339\u914d\u9879\u3002\u8fd9\u79cd\u65b9\u5f0f\u5e38\u7528\u4e8e\u9700\u8981\u591a\u6b21\u4f7f\u7528\u540c\u4e00\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u60c5\u51b5\uff0c\u53ef\u4ee5\u63d0\u9ad8\u6548\u7387\u3002<\/p>\n<\/li>\n<\/ol>\n<p>\u4f7f\u7528\u4e0a\u8ff0\u65b9\u6cd5\u4e2d\u7684\u4efb\u4f55\u4e00\u79cd\uff0c\u53ef\u4ee5\u5b9e\u73b0\u5339\u914d\u5230\u6240\u6709\u7b26\u5408\u9879\u7684\u529f\u80fd\uff0c\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u5373\u53ef\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u4e00\u79cd\u7528\u6765\u4ece\u5b57\u7b26\u4e32\u4e2d\u63d0\u53d6\u7b26\u5408\u67d0\u4e2a\u6a21\u5f0f\u7684\u6587\u672c\u7684\u5de5\u5177\u3002\u5728Python\u4e2d\uff0c\u8981\u5b9e\u73b0\u5339\u914d\u5230\u6240\u6709\u7b26\u5408\u9879\uff0c\u6211\u4eec\u53ef\u4ee5 [&hellip;]","protected":false},"author":3,"featured_media":176897,"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\/176894"}],"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=176894"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/176894\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/176897"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=176894"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=176894"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=176894"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}