{"id":176780,"date":"2024-05-08T19:17:50","date_gmt":"2024-05-08T11:17:50","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/176780.html"},"modified":"2024-05-08T19:18:01","modified_gmt":"2024-05-08T11:18:01","slug":"python-%e6%ad%a3%e5%88%99%e8%a1%a8%e8%be%be%e5%bc%8f-re-m-%e6%80%8e%e4%b9%88%e4%b8%8d%e7%ae%a1%e7%94%a8%e5%91%a2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/176780.html","title":{"rendered":"Python \u6b63\u5219\u8868\u8fbe\u5f0f re.M \u600e\u4e48\u4e0d\u7ba1\u7528\u5462"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27060518\/0d95f8d5-c0df-40e6-abf1-c94f959cf101.webp\" alt=\"Python \u6b63\u5219\u8868\u8fbe\u5f0f re.M \u600e\u4e48\u4e0d\u7ba1\u7528\u5462\" \/><\/p>\n<p><p><strong>Python \u6b63\u5219\u8868\u8fbe\u5f0f\u4e2d\u7684re.M\uff08\u591a\u884c\u6a21\u5f0f\uff09\u5b9e\u9645\u4e0a\u662f\u975e\u5e38\u6709\u6548\u7684\uff0c\u5b83\u5141\u8bb8&quot;^&quot;\u548c&quot;$&quot;\u5339\u914d\u6bcf\u4e00\u884c\u7684\u5f00\u59cb\u548c\u7ed3\u675f\u4f4d\u7f6e\u3001\u5373\u4fbf\u5728\u591a\u884c\u5b57\u7b26\u4e32\u4e2d\u3002<\/strong> \u5f53\u4f60\u9762\u4e34re.M\u4f3c\u4e4e\u4e0d\u7ba1\u7528\u7684\u60c5\u51b5\u65f6\uff0c\u901a\u5e38\u662f\u56e0\u4e3a\u4e0d\u719f\u6089\u5176\u5de5\u4f5c\u673a\u5236\u6216\u8005\u5728\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u7f16\u5199\u4e0a\u5b58\u5728\u8bef\u533a\u3002\u5728\u591a\u884c\u6a21\u5f0f\u4e0b\uff0c&quot;^&quot;\u5339\u914d\u4e0d\u4ec5\u4f1a\u51fa\u73b0\u5728\u6574\u4e2a\u5b57\u7b26\u4e32\u7684\u8d77\u59cb\u5904\uff0c\u4e5f\u4f1a\u5728\u6bcf\u4e00\u884c\u7684\u5f00\u59cb\uff0c\u800c&quot;$&quot;\u4f1a\u5339\u914d\u6574\u4e2a\u5b57\u7b26\u4e32\u7684\u7ed3\u5c3e\u4ee5\u53ca\u6bcf\u4e00\u884c\u7684\u7ed3\u5c3e\u5904\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001re.M\u7684\u5de5\u4f5c\u539f\u7406<\/h3>\n<\/p>\n<p><p>re.M\u6216re.MULTILINE\u662f\u4e00\u4e2a\u5728\u7f16\u8bd1\u6b63\u5219\u8868\u8fbe\u5f0f\u65f6\u4f7f\u7528\u7684\u6807\u5fd7\uff0c\u5b83\u6539\u53d8Python\u4e2d&quot;^&quot;\u548c&quot;$&quot;\u5143\u5b57\u7b26\u7684\u8868\u73b0\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c&quot;^&quot;\u5339\u914d\u5b57\u7b26\u4e32\u7684\u5f00\u5934\uff0c&quot;$&quot;\u5339\u914d\u5b57\u7b26\u4e32\u7684\u7ed3\u5c3e\u3002\u4f46\u662f\uff0c\u542f\u7528\u4e86re.M\u6807\u5fd7\u540e\uff0c&quot;^&quot;\u4f1a\u5339\u914d\u6bcf\u4e00\u884c\u7684\u5f00\u5934\u548c\u6574\u4e2a\u5b57\u7b26\u4e32\u7684\u5f00\u5934\uff0c&quot;$&quot;\u4f1a\u5339\u914d\u6bcf\u4e00\u884c\u7684\u7ed3\u5c3e\u548c\u6574\u4e2a\u5b57\u7b26\u4e32\u7684\u7ed3\u5c3e\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4e00\uff1a\u4e0d\u4f7f\u7528re.M<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u6211\u4eec\u6709\u4e00\u4e2a\u591a\u884c\u5b57\u7b26\u4e32\uff0c\u6211\u4eec\u60f3\u627e\u5230\u6bcf\u884c\u5f00\u5934\u51fa\u73b0\u7684&quot;Python&quot;\uff1a<\/p>\n<\/p>\n<p><pre><code>import re<\/p>\n<p>text = &quot;&quot;&quot;Python is an interpreted, high-level language.<\/p>\n<p>Python is named after Monty Python.<\/p>\n<p>Python syntax is very clean.&quot;&quot;&quot;<\/p>\n<p>pattern = &quot;^Python&quot;<\/p>\n<p>matches = re.findall(pattern, text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c<code>findall<\/code>\u51fd\u6570\u53ea\u4f1a\u8fd4\u56de\u4e00\u4e2a\u5339\u914d\uff0c\u56e0\u4e3a\u5b83\u53ea\u5339\u914d\u6574\u4e2a\u5b57\u7b26\u4e32\u7684\u5f00\u59cb\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4e8c\uff1a\u4f7f\u7528re.M<\/h4>\n<\/p>\n<p><pre><code>pattern = &quot;^Python&quot;<\/p>\n<p>matches = re.findall(pattern, text, re.M)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u542f\u7528<code>re.M<\/code>\u4e4b\u540e\uff0c<code>findall<\/code>\u51fd\u6570\u4f1a\u5728\u6bcf\u884c\u7684\u8d77\u59cb\u5904\u5bfb\u627e&quot;Python&quot;\uff0c\u8fd4\u56de\u4e09\u6b21\u5339\u914d\uff0c\u6bcf\u884c\u4e00\u6b21\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u5e38\u89c1\u7684re.M\u8bef\u7528<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u4e2d\uff0c\u5f53\u4f60\u89c9\u5f97re.M\u201c\u4e0d\u7ba1\u7528\u201d\u65f6\uff0c\u53ef\u80fd\u662f\u4ee5\u4e0b\u51e0\u4e2a\u539f\u56e0\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u6df7\u6dc6\u4e86\u884c\u4e0e\u5b57\u7b26\u4e32\u5f00\u5934\/\u7ed3\u5c3e\uff1a<\/strong> \u5982\u679c\u4f60\u6ca1\u6709\u6b63\u786e\u5730\u7406\u89e3\u884c\u4e0e\u5b57\u7b26\u4e32\u5f00\u5934\/\u7ed3\u5c3e\u7684\u533a\u522b\uff0c\u53ef\u80fd\u4f1a\u8bef\u8ba4\u4e3are.M\u6ca1\u6709\u751f\u6548\u3002<\/li>\n<li><strong>\u6a21\u5f0f\u5b57\u7b26\u4e32\u672a\u6b63\u786e\u5b9a\u4e49\uff1a<\/strong> \u6709\u65f6\uff0c\u7531\u4e8e\u6a21\u5f0f\u5b57\u7b26\u4e32\u5b9a\u4e49\u4e0d\u5f53\uff08\u5982\u5fd8\u8bb0\u4e86\u6362\u884c\u7b26\u7b49\uff09\uff0c\u53ef\u80fd\u5bfc\u81f4\u9884\u671f\u4e4b\u5916\u7684\u5339\u914d\u7ed3\u679c\u3002<\/li>\n<li><strong>\u591a\u884c\u5b57\u7b26\u4e32\u672c\u8eab\u7684\u683c\u5f0f\u9519\u8bef\uff1a<\/strong> \u5982\u679c\u5b57\u7b26\u4e32\u672c\u8eab\u5c31\u4e0d\u662f\u591a\u884c\u7684\uff0c\u6216\u8005\u5904\u7406\u7684\u6587\u672c\u4e0d\u5305\u542b\u9884\u671f\u7684\u884c\u7ec8\u6b62\u7b26\uff0cre.M\u4e0d\u4f1a\u5982\u9884\u671f\u5de5\u4f5c\u3002<\/li>\n<li><strong>\u6df7\u7528\u5176\u4ed6\u6a21\u5f0f\u4fee\u9970\u7b26\uff1a<\/strong> \u6709\u65f6\u53ef\u80fd\u4e0d\u5c0f\u5fc3\u6df7\u7528\u4e86\u5176\u4ed6\u6a21\u5f0f\u4fee\u9970\u7b26\uff08\u6bd4\u5982re.DOTALL\uff09\uff0c\u8fd9\u4e5f\u53ef\u80fd\u5f15\u8d77\u6df7\u4e71\u3002<\/li>\n<\/ol>\n<p><h3>\u4e09\u3001\u68c0\u67e5\u6b63\u5219\u8868\u8fbe\u5f0f<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528re.M\u65f6\uff0c\u786e\u4fdd\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u9488\u5bf9\u591a\u884c\u6587\u672c\u51c6\u786e\u7f16\u5199\u7684\u3002\u4f8b\u5982\uff0c\u786e\u4fdd\u6587\u672c\u4f7f\u7528\u6b63\u786e\u7684\u6362\u884c\u7b26\uff0c\u5e76\u4e14\u6b63\u5219\u8868\u8fbe\u5f0f\u80fd\u6b63\u786e\u5730\u5339\u914d\u3002<\/p>\n<\/p>\n<p><h4>\u68c0\u67e5\u6362\u884c\u7b26<\/h4>\n<\/p>\n<p><p>\u591a\u884c\u5b57\u7b26\u4e32\u4e2d\u901a\u5e38\u4f1a\u5305\u542b\u6362\u884c\u7b26&quot;\\n&quot;\u3002\u786e\u4fdd\u4f60\u7684\u5b57\u7b26\u4e32\u4e2d\u786e\u5b9e\u6709\u8fd9\u4e9b\u6362\u884c\u7b26\uff0c\u5e76\u4e14\u5b83\u4eec\u653e\u7f6e\u5728\u6b63\u786e\u7684\u4f4d\u7f6e\u3002<\/p>\n<\/p>\n<p><h4>\u51c6\u786e\u5339\u914d\u6a21\u5f0f<\/h4>\n<\/p>\n<p><p>\u4e3a\u5b9e\u73b0\u51c6\u786e\u5339\u914d\uff0c\u53ef\u80fd\u9700\u8981\u7ec6\u5fc3\u5730\u6784\u9020\u6b63\u5219\u8868\u8fbe\u5f0f\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u60f3\u5339\u914d\u6bcf\u884c\u7684\u5f00\u5934\u662f\u4e0d\u662f\u6570\u5b57\uff0c\u4f60\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u53ef\u80fd\u4f1a\u662f<code>&quot;^\\d&quot;<\/code>\uff0c\u5e76\u914d\u5408re.M\u6765\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5b9e\u9645\u5e94\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><p>\u5728\u591a\u884c\u6587\u672c\u5904\u7406\u7684\u573a\u666f\u4e2d\uff0cre.M\u662f\u975e\u5e38\u6709\u7528\u7684\u3002\u5b83\u5141\u8bb8\u4f60\u5bf9\u6bcf\u4e00\u884c\u5355\u72ec\u8fdb\u884c\u5904\u7406\uff0c\u6bd4\u5982\u8bf4\u5728\u65e5\u5fd7\u6587\u4ef6\u5206\u6790\u3001\u591a\u884c\u5b57\u7b26\u4e32\u6570\u636e\u7684\u63d0\u53d6\u7b49\u573a\u666f\u4e2d\u3002<\/p>\n<\/p>\n<p><h4>\u65e5\u5fd7\u6587\u4ef6\u5206\u6790<\/h4>\n<\/p>\n<p><p>\u5047\u8bbe\u4f60\u6709\u4e00\u4efd\u65e5\u5fd7\u6587\u4ef6\uff0c\u6bcf\u6761\u65e5\u5fd7\u90fd\u662f\u5728\u65b0\u7684\u4e00\u884c\u5f00\u59cb\uff0c\u4f60\u60f3\u8981\u63d0\u53d6\u5176\u4e2d\u6240\u6709\u7684\u65e5\u671f\u6807\u8bb0\uff0c\u8fd9\u65f6\u7528\u5230re.M\u53ef\u4ee5\u8ba9\u4f60\u8f7b\u677e\u5730\u5339\u914d\u6bcf\u884c\u7684\u5f00\u59cb\u3002<\/p>\n<\/p>\n<p><h4>\u6570\u636e\u63d0\u53d6<\/h4>\n<\/p>\n<p><p>\u5728\u5904\u7406\u7531\u591a\u884c\u7ec4\u6210\u7684\u5b57\u7b26\u4e32\u6570\u636e\u65f6\uff0c\u4f8b\u5982\u4ece\u90ae\u4ef6\u5185\u5bb9\u6216\u8005\u6587\u6863\u4e2d\u63d0\u53d6\u4fe1\u606f\uff0c\u4f7f\u7528re.M\u80fd\u591f\u5e2e\u52a9\u6211\u4eec\u66f4\u597d\u5730\u5b9a\u4e49\u5339\u914d\u89c4\u5219\uff0c\u63d0\u53d6\u6bcf\u4e00\u884c\u7684\u5173\u952e\u6570\u636e\u3002<\/p>\n<\/p>\n<p><p>\u603b\u7684\u6765\u8bf4\uff0c<strong>re.M\u662f\u4e00\u4e2a\u5728\u5904\u7406\u591a\u884c\u6587\u672c\u65f6\u975e\u5e38\u5f3a\u5927\u7684\u5de5\u5177<\/strong>\u3002\u6b63\u786e\u5730\u4f7f\u7528\u5b83\u9700\u8981\u4e00\u5b9a\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u77e5\u8bc6\u548c\u5bf9Python re\u6a21\u5757\u7684\u7406\u89e3\u3002\u5f53\u611f\u89c9\u5b83&quot;\u4e0d\u7ba1\u7528&quot;\u65f6\uff0c\u56de\u987e\u4e00\u4e0b\u5b83\u7684\u5de5\u4f5c\u673a\u5236\u548c\u4f60\u7684\u4ee3\u7801\u5b9e\u73b0\uff0c\u5f80\u5f80\u80fd\u627e\u5230\u95ee\u9898\u6240\u5728\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>\u4e3a\u4ec0\u4e48\u4f7f\u7528 Python \u7684 re.M\uff08\u591a\u884c\u5339\u914d\uff09\u6a21\u5f0f\u65f6\u65e0\u6cd5\u6b63\u5e38\u5de5\u4f5c\uff1f<\/strong><br \/>\n\u53ef\u80fd\u6709\u591a\u79cd\u539f\u56e0\u5bfc\u81f4\u4f7f\u7528 re.M \u6a21\u5f0f\u65f6\u65e0\u6cd5\u6b63\u5e38\u5de5\u4f5c\u3002\u4e00\u79cd\u53ef\u80fd\u7684\u539f\u56e0\u662f\u6b63\u5219\u8868\u8fbe\u5f0f\u672c\u8eab\u7684\u95ee\u9898\u3002\u8bf7\u786e\u4fdd\u6b63\u5219\u8868\u8fbe\u5f0f\u672c\u8eab\u662f\u5426\u6b63\u786e\uff0c\u5e76\u4e14\u7b26\u5408\u9884\u671f\u7684\u5339\u914d\u89c4\u5219\u3002\u53e6\u4e00\u4e2a\u539f\u56e0\u53ef\u80fd\u662f\u6240\u5904\u7406\u7684\u6587\u672c\u6570\u636e\u4e2d\u4e0d\u5b58\u5728\u4e0e\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u7684\u5185\u5bb9\u3002\u60a8\u53ef\u4ee5\u5c1d\u8bd5\u4f7f\u7528\u4e0d\u540c\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u6216\u68c0\u67e5\u6570\u636e\u7684\u7ed3\u6784\u662f\u5426\u4e0e\u9884\u671f\u5339\u914d\u3002\u8fd8\u6709\u4e00\u79cd\u53ef\u80fd\u662f\u5728\u8c03\u7528 re.M \u6a21\u5f0f\u65f6\uff0c\u4f20\u5165\u7684\u53c2\u6570\u6709\u9519\u8bef\u3002\u8bf7\u786e\u4fdd\u6b63\u786e\u4f7f\u7528 re.compile(pattern, flags=re.M) \u8fd9\u6837\u7684\u8bed\u6cd5\u3002<\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528 Python \u7684 re.M\uff08\u591a\u884c\u5339\u914d\uff09\u6a21\u5f0f\u8fdb\u884c\u6587\u672c\u5339\u914d\uff1f<\/strong><br \/>\n\u4f7f\u7528 re.M\uff08\u591a\u884c\u5339\u914d\uff09\u6a21\u5f0f\u53ef\u4ee5\u4f7f\u5f97\u6b63\u5219\u8868\u8fbe\u5f0f\u5728\u5339\u914d\u8fc7\u7a0b\u4e2d\u8de8\u8d8a\u591a\u884c\uff0c\u800c\u4e0d\u4ec5\u4ec5\u662f\u5355\u884c\u3002\u5728\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u65f6\uff0c\u53ef\u4ee5\u5728\u8c03\u7528 re.compile(pattern, flags=re.M) \u65b9\u6cd5\u65f6\u4f20\u9012 re.M \u4f5c\u4e3a flags \u53c2\u6570\u7684\u503c\u6765\u542f\u7528\u591a\u884c\u5339\u914d\u6a21\u5f0f\u3002\u4e4b\u540e\uff0c\u4f7f\u7528 re.match()\u3001re.search() \u6216 re.findall() \u7b49\u51fd\u6570\u8fdb\u884c\u6587\u672c\u5339\u914d\u65f6\uff0c\u5c31\u4f1a\u540c\u65f6\u8003\u8651\u591a\u884c\u7684\u60c5\u51b5\u3002<\/p>\n<p><strong>\u6709\u6ca1\u6709\u5176\u4ed6\u66ff\u4ee3\u65b9\u6cd5\u53ef\u4ee5\u5b9e\u73b0\u7c7b\u4f3c re.M\uff08\u591a\u884c\u5339\u914d\uff09\u6a21\u5f0f\u7684\u529f\u80fd\uff1f<\/strong><br \/>\n\u5982\u679c\u60a8\u5728\u4f7f\u7528 re.M \u6a21\u5f0f\u65f6\u9047\u5230\u95ee\u9898\uff0c\u8fd8\u53ef\u4ee5\u5c1d\u8bd5\u4f7f\u7528\u5176\u4ed6\u66ff\u4ee3\u65b9\u6cd5\u6765\u5b9e\u73b0\u7c7b\u4f3c\u7684\u529f\u80fd\u3002\u4e00\u79cd\u65b9\u6cd5\u662f\u4f7f\u7528 re.DOTALL \u6a21\u5f0f\uff0c\u5b83\u4f1a\u5c06\u70b9\u5b57\u7b26\uff08.\uff09\u89c6\u4e3a\u5305\u62ec\u6362\u884c\u7b26\u5728\u5185\u7684\u4efb\u610f\u5b57\u7b26\u3002\u8fd9\u6837\u5c31\u53ef\u4ee5\u5b9e\u73b0\u5728\u8de8\u8d8a\u591a\u884c\u7684\u60c5\u51b5\u4e0b\u5339\u914d\u7279\u5b9a\u7684\u6587\u672c\u6a21\u5f0f\u3002\u53e6\u4e00\u79cd\u65b9\u6cd5\u662f\u4f7f\u7528\u5b57\u7b26\u4e32\u7684 split() \u65b9\u6cd5\u6765\u5206\u5272\u6587\u672c\uff0c\u5e76\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5bf9\u5206\u5272\u540e\u7684\u6587\u672c\u8fdb\u884c\u5355\u884c\u5339\u914d\u3002\u8fd9\u6837\u53ef\u4ee5\u8fbe\u5230\u7c7b\u4f3c\u7684\u6548\u679c\u3002\u4e0d\u540c\u7684\u65b9\u6cd5\u9002\u7528\u4e8e\u4e0d\u540c\u7684\u60c5\u51b5\uff0c\u60a8\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python \u6b63\u5219\u8868\u8fbe\u5f0f\u4e2d\u7684re.M\uff08\u591a\u884c\u6a21\u5f0f\uff09\u5b9e\u9645\u4e0a\u662f\u975e\u5e38\u6709\u6548\u7684\uff0c\u5b83\u5141\u8bb8&quot;^&quot;\u548c&#038;quo [&hellip;]","protected":false},"author":3,"featured_media":176802,"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\/176780"}],"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=176780"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/176780\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/176802"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=176780"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=176780"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=176780"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}