{"id":1100222,"date":"2025-01-08T15:37:59","date_gmt":"2025-01-08T07:37:59","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1100222.html"},"modified":"2025-01-08T15:38:01","modified_gmt":"2025-01-08T07:38:01","slug":"python%e5%a6%82%e4%bd%95%e5%af%b9%e5%8f%82%e6%95%b0%e5%ae%9e%e7%8e%b0%e5%8a%a0r%e6%95%88%e6%9e%9c-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1100222.html","title":{"rendered":"python\u5982\u4f55\u5bf9\u53c2\u6570\u5b9e\u73b0\u52a0r\u6548\u679c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25063555\/531ebfc6-b00a-4b18-910c-f293b876077f.webp\" alt=\"python\u5982\u4f55\u5bf9\u53c2\u6570\u5b9e\u73b0\u52a0r\u6548\u679c\" \/><\/p>\n<p><p> <strong>\u8981\u5728Python\u4e2d\u5bf9\u53c2\u6570\u5b9e\u73b0\u52a0r\u6548\u679c\uff0c\u53ef\u4ee5\u4f7f\u7528\u539f\u59cb\u5b57\u7b26\u4e32\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u3001\u8f6c\u4e49\u5b57\u7b26\u7b49\u65b9\u6cd5\u3002<\/strong> \u4f7f\u7528\u539f\u59cb\u5b57\u7b26\u4e32\uff08raw string\uff09\u662f\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\uff0c\u5b83\u53ef\u4ee5\u901a\u8fc7\u5728\u5b57\u7b26\u4e32\u524d\u52a0\u4e0a\u5b57\u6bcd&#39;r&#39;\u6765\u5b9e\u73b0\uff0c\u8be5\u5b57\u7b26\u4e32\u4e2d\u7684\u6240\u6709\u53cd\u659c\u6760\u90fd\u4e0d\u4f1a\u88ab\u89e3\u91ca\u4e3a\u8f6c\u4e49\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5b57\u7b26\u4e32\u524d\u52a0&#39;r&#39;\u7684\u4e3b\u8981\u7528\u9014\u662f\u5728\u5904\u7406\u6b63\u5219\u8868\u8fbe\u5f0f\u65f6\uff0c\u907f\u514d\u53cd\u659c\u6760\u88ab\u89e3\u91ca\u4e3a\u8f6c\u4e49\u5b57\u7b26\u3002\u539f\u59cb\u5b57\u7b26\u4e32\u786e\u4fdd\u6240\u6709\u7684\u53cd\u659c\u6760\u90fd\u4fdd\u7559\u5728\u5b57\u7b26\u4e32\u4e2d\uff0c\u800c\u4e0d\u662f\u88ab\u89e3\u91ca\u4e3a\u7279\u6b8a\u5b57\u7b26\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u8ba8\u8bba\u5982\u4f55\u4f7f\u7528\u539f\u59cb\u5b57\u7b26\u4e32\u4ee5\u53ca\u5176\u4ed6\u76f8\u5173\u6280\u672f\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u539f\u59cb\u5b57\u7b26\u4e32\uff08Raw String\uff09<\/h3>\n<\/p>\n<p><p>\u539f\u59cb\u5b57\u7b26\u4e32\u5728Python\u4e2d\u901a\u8fc7\u5728\u5b57\u7b26\u4e32\u524d\u9762\u52a0\u4e0a\u5b57\u6bcd&#39;r&#39;\u6765\u5b9e\u73b0\uff0c\u4f8b\u5982\uff1a<code>r&quot;string&quot;<\/code>\u3002\u8fd9\u662f\u5904\u7406\u5305\u542b\u53cd\u659c\u6760\u7684\u5b57\u7b26\u4e32\u6700\u76f4\u63a5\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528\u539f\u59cb\u5b57\u7b26\u4e32<\/h4>\n<\/p>\n<p><p>\u5f53\u5904\u7406\u6587\u4ef6\u8def\u5f84\u6216\u6b63\u5219\u8868\u8fbe\u5f0f\u65f6\uff0c\u539f\u59cb\u5b57\u7b26\u4e32\u975e\u5e38\u6709\u7528\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6587\u4ef6\u8def\u5f84\u793a\u4f8b<\/p>\n<p>path = r&quot;C:\\Users\\Example\\Documents\\file.txt&quot;<\/p>\n<p>print(path)  # \u8f93\u51fa\uff1aC:\\Users\\Example\\Documents\\file.txt<\/p>\n<h2><strong>\u6b63\u5219\u8868\u8fbe\u5f0f\u793a\u4f8b<\/strong><\/h2>\n<p>import re<\/p>\n<p>pattern = r&quot;\\d{3}-\\d{2}-\\d{4}&quot;<\/p>\n<p>text = &quot;My number is 123-45-6789.&quot;<\/p>\n<p>match = re.search(pattern, text)<\/p>\n<p>if match:<\/p>\n<p>    print(&quot;Found:&quot;, match.group())  # \u8f93\u51fa\uff1aFound: 123-45-6789<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4f7f\u7528\u539f\u59cb\u5b57\u7b26\u4e32\uff0c\u6211\u4eec\u907f\u514d\u4e86\u5fc5\u987b\u5bf9\u6bcf\u4e2a\u53cd\u659c\u6760\u8fdb\u884c\u8f6c\u4e49\uff0c\u4f7f\u4ee3\u7801\u66f4\u52a0\u6e05\u6670\u6613\u8bfb\u3002<\/p>\n<\/p>\n<p><h4>2. \u591a\u884c\u539f\u59cb\u5b57\u7b26\u4e32<\/h4>\n<\/p>\n<p><p>\u591a\u884c\u5b57\u7b26\u4e32\u4e5f\u53ef\u4ee5\u4f7f\u7528\u539f\u59cb\u5b57\u7b26\u4e32\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u7ed3\u5408\u4e09\u91cd\u5f15\u53f7\u4f7f\u7528\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u591a\u884c\u539f\u59cb\u5b57\u7b26\u4e32<\/p>\n<p>multi_line_string = r&quot;&quot;&quot;<\/p>\n<p>This is a raw string example.<\/p>\n<p>Path: C:\\Users\\Example<\/p>\n<p>Regex: \\d{3}-\\d{2}-\\d{4}<\/p>\n<p>&quot;&quot;&quot;<\/p>\n<p>print(multi_line_string)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u4e2d\u7684\u539f\u59cb\u5b57\u7b26\u4e32<\/h3>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u539f\u59cb\u5b57\u7b26\u4e32\u6700\u5e38\u89c1\u7684\u5e94\u7528\u573a\u666f\u4e4b\u4e00\u3002\u5728\u6b63\u5219\u8868\u8fbe\u5f0f\u4e2d\uff0c\u53cd\u659c\u6760\u88ab\u5e7f\u6cdb\u4f7f\u7528\u6765\u8868\u793a\u7279\u6b8a\u5b57\u7b26\u6216\u8f6c\u4e49\u5e8f\u5217\uff0c\u56e0\u6b64\u4f7f\u7528\u539f\u59cb\u5b57\u7b26\u4e32\u53ef\u4ee5\u907f\u514d\u8f6c\u4e49\u53cd\u659c\u6760\u7684\u9ebb\u70e6\u3002<\/p>\n<\/p>\n<p><h4>1. \u57fa\u672c\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u793a\u4f8b<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>pattern = r&quot;\\b[A-Za-z]+\\b&quot;<\/p>\n<p>text = &quot;This is a test.&quot;<\/p>\n<p>matches = re.findall(pattern, text)<\/p>\n<p>print(matches)  # \u8f93\u51fa\uff1a[&#39;This&#39;, &#39;is&#39;, &#39;a&#39;, &#39;test&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>r&quot;\\b[A-Za-z]+\\b&quot;<\/code>\u662f\u4e00\u4e2a\u539f\u59cb\u5b57\u7b26\u4e32\uff0c\u5b83\u8868\u793a\u4e00\u4e2a\u5355\u8bcd\u8fb9\u754c\u548c\u4e00\u4e2a\u6216\u591a\u4e2a\u5b57\u6bcd\u3002<\/p>\n<\/p>\n<p><h4>2. \u4f7f\u7528\u6355\u83b7\u7ec4<\/h4>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\u4e2d\u6355\u83b7\u7ec4\u7684\u4f7f\u7528\u4e5f\u53ef\u4ee5\u901a\u8fc7\u539f\u59cb\u5b57\u7b26\u4e32\u6765\u7b80\u5316\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">pattern = r&quot;(\\d{3})-(\\d{2})-(\\d{4})&quot;<\/p>\n<p>text = &quot;My number is 123-45-6789.&quot;<\/p>\n<p>match = re.search(pattern, text)<\/p>\n<p>if match:<\/p>\n<p>    print(&quot;Area:&quot;, match.group(1))  # \u8f93\u51fa\uff1aArea: 123<\/p>\n<p>    print(&quot;Group:&quot;, match.group(2))  # \u8f93\u51fa\uff1aGroup: 45<\/p>\n<p>    print(&quot;Number:&quot;, match.group(3))  # \u8f93\u51fa\uff1aNumber: 6789<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u539f\u59cb\u5b57\u7b26\u4e32\u4f7f\u5f97\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u5b9a\u4e49\u66f4\u52a0\u76f4\u89c2\u548c\u6613\u8bfb\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u8f6c\u4e49\u5b57\u7b26<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u80fd\u9700\u8981\u624b\u52a8\u5bf9\u5b57\u7b26\u4e32\u4e2d\u7684\u53cd\u659c\u6760\u8fdb\u884c\u8f6c\u4e49\u3002\u867d\u7136\u8fd9\u4e0d\u662f\u63a8\u8350\u7684\u65b9\u6cd5\uff0c\u4f46\u5728\u67d0\u4e9b\u573a\u666f\u4e0b\u53ef\u80fd\u4f1a\u7528\u5230\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u624b\u52a8\u8f6c\u4e49\u53cd\u659c\u6760<\/p>\n<p>path = &quot;C:\\\\Users\\\\Example\\\\Documents\\\\file.txt&quot;<\/p>\n<p>print(path)  # \u8f93\u51fa\uff1aC:\\Users\\Example\\Documents\\file.txt<\/p>\n<h2><strong>\u624b\u52a8\u8f6c\u4e49\u6b63\u5219\u8868\u8fbe\u5f0f\u4e2d\u7684\u53cd\u659c\u6760<\/strong><\/h2>\n<p>import re<\/p>\n<p>pattern = &quot;\\\\d{3}-\\\\d{2}-\\\\d{4}&quot;<\/p>\n<p>text = &quot;My number is 123-45-6789.&quot;<\/p>\n<p>match = re.search(pattern, text)<\/p>\n<p>if match:<\/p>\n<p>    print(&quot;Found:&quot;, match.group())  # \u8f93\u51fa\uff1aFound: 123-45-6789<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u624b\u52a8\u8f6c\u4e49\u53cd\u659c\u6760\u4f7f\u4ee3\u7801\u53d8\u5f97\u5197\u957f\u4e14\u5bb9\u6613\u51fa\u9519\uff0c\u56e0\u6b64\u4e0d\u63a8\u8350\u4f7f\u7528\u8fd9\u79cd\u65b9\u6cd5\uff0c\u9664\u975e\u5fc5\u8981\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\u5904\u7406\u53cd\u659c\u6760\u6700\u6709\u6548\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u539f\u59cb\u5b57\u7b26\u4e32\u3002\u8fd9\u4e0d\u4ec5\u4f7f\u4ee3\u7801\u66f4\u7b80\u6d01\uff0c\u8fd8\u907f\u514d\u4e86\u624b\u52a8\u8f6c\u4e49\u5e26\u6765\u7684\u9519\u8bef\u548c\u56f0\u60d1\u3002<strong>\u539f\u59cb\u5b57\u7b26\u4e32\uff08raw string\uff09\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u3001\u8f6c\u4e49\u5b57\u7b26<\/strong>\u662f\u5904\u7406\u8fd9\u4e9b\u95ee\u9898\u7684\u4e3b\u8981\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4f7f\u7528\u539f\u59cb\u5b57\u7b26\u4e32\uff0c\u60a8\u53ef\u4ee5\u786e\u4fdd\u6240\u6709\u53cd\u659c\u6760\u90fd\u4fdd\u7559\u5728\u5b57\u7b26\u4e32\u4e2d\uff0c\u800c\u4e0d\u662f\u88ab\u89e3\u91ca\u4e3a\u7279\u6b8a\u5b57\u7b26\uff0c\u4ece\u800c\u4f7f\u4ee3\u7801\u66f4\u6613\u4e8e\u7f16\u5199\u548c\u7ef4\u62a4\u3002\u65e0\u8bba\u662f\u5904\u7406\u6587\u4ef6\u8def\u5f84\u8fd8\u662f\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u539f\u59cb\u5b57\u7b26\u4e32\u90fd\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u4f7f\u5f97\u60a8\u7684Python\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u548c\u9ad8\u6548\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528\u539f\u59cb\u5b57\u7b26\u4e32\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u5728\u5b57\u7b26\u4e32\u524d\u52a0\u4e0a\u5b57\u6bcd&#39;r&#39;\u6216&#39;R&#39;\u6765\u521b\u5efa\u539f\u59cb\u5b57\u7b26\u4e32\u3002\u539f\u59cb\u5b57\u7b26\u4e32\u4f1a\u5ffd\u7565\u8f6c\u4e49\u5b57\u7b26\u7684\u5f71\u54cd\uff0c\u8fd9\u610f\u5473\u7740\u53cd\u659c\u6760\u4e0d\u4f1a\u88ab\u89c6\u4e3a\u8f6c\u4e49\u7b26\u3002\u4f8b\u5982\uff0cr&quot;\u8fd9\u662f\u4e00\u4e2a\\n\u539f\u59cb\u5b57\u7b26\u4e32&quot;\u4f1a\u88ab\u89c6\u4e3a&quot;\u8fd9\u662f\u4e00\u4e2a\\n\u539f\u59cb\u5b57\u7b26\u4e32&quot;\uff0c\u800c\u4e0d\u662f\u6362\u884c\u7b26\u3002\u4f7f\u7528\u539f\u59cb\u5b57\u7b26\u4e32\u5728\u5904\u7406\u6b63\u5219\u8868\u8fbe\u5f0f\u548c\u6587\u4ef6\u8def\u5f84\u65f6\u7279\u522b\u6709\u7528\u3002<\/p>\n<p><strong>\u4f7f\u7528\u539f\u59cb\u5b57\u7b26\u4e32\u5bf9\u6b63\u5219\u8868\u8fbe\u5f0f\u6709\u4ec0\u4e48\u597d\u5904\uff1f<\/strong><br \/>\u4f7f\u7528\u539f\u59cb\u5b57\u7b26\u4e32\u5b9a\u4e49\u6b63\u5219\u8868\u8fbe\u5f0f\u65f6\uff0c\u53ef\u4ee5\u907f\u514d\u53cd\u659c\u6760\u7684\u53cc\u91cd\u8f6c\u4e49\u95ee\u9898\u3002\u4f8b\u5982\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f\u4e2d\u7684&quot;\\d&quot;\u8868\u793a\u6570\u5b57\uff0c\u5982\u679c\u4e0d\u4f7f\u7528\u539f\u59cb\u5b57\u7b26\u4e32\uff0c\u9700\u8981\u5199\u6210&quot;\\d&quot;\u3002\u4f7f\u7528\u539f\u59cb\u5b57\u7b26\u4e32\u53ef\u4ee5\u4f7f\u4ee3\u7801\u66f4\u7b80\u6d01\uff0c\u6613\u4e8e\u9605\u8bfb\u548c\u7ef4\u62a4\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u5c06\u666e\u901a\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u539f\u59cb\u5b57\u7b26\u4e32\uff1f<\/strong><br \/>\u867d\u7136\u5728Python\u4e2d\u65e0\u6cd5\u76f4\u63a5\u5c06\u4e00\u4e2a\u666e\u901a\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u539f\u59cb\u5b57\u7b26\u4e32\uff0c\u4f46\u53ef\u4ee5\u901a\u8fc7\u6dfb\u52a0\u524d\u7f00&#39;r&#39;\u6765\u5b9a\u4e49\u65b0\u7684\u539f\u59cb\u5b57\u7b26\u4e32\u3002\u5982\u679c\u4f60\u5df2\u7ecf\u6709\u4e00\u4e2a\u5b57\u7b26\u4e32\u5e76\u60f3\u8981\u5728\u4f7f\u7528\u65f6\u5c06\u5176\u89c6\u4e3a\u539f\u59cb\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u5728\u4f7f\u7528\u5b83\u7684\u5730\u65b9\u52a0\u4e0ar\u524d\u7f00\uff0c\u6216\u8005\u5728\u5904\u7406\u65f6\u4f7f\u7528<code>re.escape()<\/code>\u51fd\u6570\u6765\u81ea\u52a8\u8f6c\u4e49\u5176\u4e2d\u7684\u7279\u6b8a\u5b57\u7b26\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u5728Python\u4e2d\u5bf9\u53c2\u6570\u5b9e\u73b0\u52a0r\u6548\u679c\uff0c\u53ef\u4ee5\u4f7f\u7528\u539f\u59cb\u5b57\u7b26\u4e32\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u3001\u8f6c\u4e49\u5b57\u7b26\u7b49\u65b9\u6cd5\u3002 \u4f7f\u7528\u539f\u59cb\u5b57\u7b26\u4e32\uff08raw [&hellip;]","protected":false},"author":3,"featured_media":1100229,"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\/1100222"}],"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=1100222"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1100222\/revisions"}],"predecessor-version":[{"id":1100232,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1100222\/revisions\/1100232"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1100229"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1100222"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1100222"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1100222"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}