{"id":3072,"date":"2025-05-29T15:59:34","date_gmt":"2025-06-04T08:59:34","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=3072"},"modified":"2025-06-04T15:59:34","modified_gmt":"2025-06-04T08:59:34","slug":"%e4%ba%86%e8%a7%a3python%e4%b8%ad%e7%9a%84sub%e5%87%bd%e6%95%b8","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/python\/%e4%ba%86%e8%a7%a3python%e4%b8%ad%e7%9a%84sub%e5%87%bd%e6%95%b8\/","title":{"rendered":"\u6df1\u5165\u4e86\u89e3 Python \u4e2d\u7684 sub() \u51fd\u6578\uff1a2025 \u6700\u65b0\u8a9e\u6cd5\u8207\u6700\u4f73\u5be6\u8e10"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, sub()\"><\/p>\n<h1>\u6df1\u5165\u4e86\u89e3 Python \u4e2d\u7684 sub() \u51fd\u6578<\/h1>\n<p>Python \u4e2d\u7684 `sub()` \u51fd\u6578\u662f\u9032\u884c\u5b57\u4e32\u66ff\u63db\u7684\u5f37\u5927\u5de5\u5177\u3002\u7121\u8ad6\u662f\u7c21\u55ae\u7684\u5b57\u4e32\u66ff\u63db\u9084\u662f\u8907\u96dc\u7684\u6b63\u5247\u8868\u9054\u5f0f\u61c9\u7528\uff0c\u9019\u500b\u51fd\u6578\u90fd\u80fd\u5e6b\u52a9\u4f60\u9ad8\u6548\u5730\u8655\u7406\u5b57\u4e32\u3002\u672c\u6587\u5c07\u5e36\u4f60\u6df1\u5165\u4e86\u89e3 `sub()` \u51fd\u6578\u7684\u7528\u6cd5\u3001\u793a\u4f8b\u4ee5\u53ca\u5e38\u898b\u932f\u8aa4\u6392\u9664\uff0c\u78ba\u4fdd\u4f60\u80fd\u5920\u9748\u6d3b\u904b\u7528\u3002<\/p>\n<p>## 1. sub() \u51fd\u6578\u7684\u57fa\u672c\u8a9e\u6cd5<\/p>\n<p>`sub()` \u51fd\u6578\u7684\u8a9e\u6cd5\u5982\u4e0b\uff1a<\/p>\n<p>&#8220;`python<br \/>\nre.sub(pattern, repl, string, count=0, flags=0)<br \/>\n&#8220;`<\/p>\n<p>&#8211; **pattern**\uff1a\u8981\u641c\u5c0b\u7684\u5b57\u4e32\u6216\u6b63\u5247\u8868\u9054\u5f0f\u3002<br \/>\n&#8211; **repl**\uff1a\u8981\u66ff\u63db\u7684\u5b57\u4e32\u3002<br \/>\n&#8211; **string**\uff1a\u8981\u88ab\u641c\u5c0b\u7684\u5b57\u4e32\u3002<br \/>\n&#8211; **count**\uff1a\u66ff\u63db\u7684\u6b21\u6578\uff0c\u9810\u8a2d\u70ba 0\uff08\u66ff\u63db\u6240\u6709\uff09\u3002<br \/>\n&#8211; **flags**\uff1a\u6a19\u8a18\uff0c\u7528\u65bc\u6307\u5b9a\u641c\u5c0b\u65b9\u5f0f\uff0c\u4f8b\u5982\u662f\u5426\u5ffd\u7565\u5927\u5c0f\u5beb\u7b49\u3002<\/p>\n<p>## 2. \u57fa\u672c\u7bc4\u4f8b<\/p>\n<p>\u8b93\u6211\u5011\u4f86\u770b\u770b\u5982\u4f55\u4f7f\u7528 `sub()` \u51fd\u6578\u9032\u884c\u57fa\u672c\u7684\u5b57\u4e32\u66ff\u63db\u3002\u5047\u8a2d\u6211\u5011\u6709\u4e00\u500b\u5b57\u4e32\uff1a<\/p>\n<p>&#8220;`python<br \/>\ns = &#8220;Hello World&#8221;<br \/>\n&#8220;`<\/p>\n<p>\u5982\u679c\u6211\u5011\u60f3\u5c07\u5176\u4e2d\u7684 &#8220;World&#8221; \u66ff\u63db\u70ba &#8220;Python&#8221;\uff0c\u53ef\u4ee5\u9019\u6a23\u505a\uff1a<\/p>\n<p>&#8220;`python<br \/>\nimport re<\/p>\n<p>s = re.sub(&#8220;World&#8221;, &#8220;Python&#8221;, s)<br \/>\n&#8220;`<\/p>\n<p>\u7d93\u904e\u9019\u6a23\u66ff\u63db\u5f8c\uff0c`s` \u7684\u503c\u5c07\u8b8a\u6210 &#8220;Hello Python&#8221;\u3002<\/p>\n<p>## 3. \u4f7f\u7528\u6b63\u5247\u8868\u9054\u5f0f<\/p>\n<p>`sub()` \u51fd\u6578\u9084\u53ef\u4ee5\u63a5\u53d7\u6b63\u5247\u8868\u9054\u5f0f\uff0c\u8b93\u6211\u5011\u80fd\u9032\u884c\u66f4\u8907\u96dc\u7684\u66ff\u63db\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6211\u5011\u60f3\u5c07\u5b57\u4e32\u4e2d\u7684\u6240\u6709\u82f1\u6587\u5b57\u6bcd\u66ff\u63db\u70ba\u6578\u5b57\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u8a9e\u6cd5\uff1a<\/p>\n<p>&#8220;`python<br \/>\ns = re.sub(&#8220;[a-zA-Z]&#8221;, &#8220;0&#8221;, s)<br \/>\n&#8220;`<\/p>\n<p>\u9019\u6a23\uff0c`s` \u7684\u503c\u5c07\u8b8a\u6210 &#8220;000 000&#8243;\u3002<\/p>\n<p>## 4. \u932f\u8aa4\u6392\u9664\u8207\u6700\u4f73\u5be6\u8e10<\/p>\n<p>\u5728\u4f7f\u7528 `sub()` \u51fd\u6578\u6642\uff0c\u53ef\u80fd\u6703\u9047\u5230\u4e00\u4e9b\u5e38\u898b\u7684\u932f\u8aa4\uff0c\u4f8b\u5982\uff1a<\/p>\n<p>&#8211; **\u672a\u5c0e\u5165 re \u6a21\u7d44**\uff1a\u78ba\u4fdd\u5728\u4f7f\u7528 `sub()` \u51fd\u6578\u524d\uff0c\u5df2\u7d93\u5c0e\u5165 `re` \u6a21\u7d44\u3002<br \/>\n&#8211; **\u6b63\u5247\u8868\u9054\u5f0f\u932f\u8aa4**\uff1a\u6b63\u5247\u8868\u9054\u5f0f\u7684\u8a9e\u6cd5\u5fc5\u9808\u6b63\u78ba\uff0c\u5426\u5247\u6703\u5f15\u767c `re.error`\u3002<\/p>\n<p>\u5efa\u8b70\u5728\u4f7f\u7528\u6b63\u5247\u8868\u9054\u5f0f\u6642\uff0c\u53ef\u4ee5\u5148\u4f7f\u7528\u7c21\u55ae\u7684\u5b57\u4e32\u66ff\u63db\u9032\u884c\u6e2c\u8a66\uff0c\u78ba\u8a8d\u7121\u8aa4\u5f8c\u518d\u9032\u884c\u8907\u96dc\u7684\u66ff\u63db\u3002<\/p>\n<p>## 5. \u5ef6\u4f38\u61c9\u7528<\/p>\n<p>`sub()` \u51fd\u6578\u4e0d\u50c5\u9650\u65bc\u5b57\u4e32\u66ff\u63db\uff0c\u9084\u53ef\u4ee5\u7528\u65bc\u6578\u64da\u6e05\u7406\u548c\u6587\u672c\u8655\u7406\u7b49\u591a\u7a2e\u61c9\u7528\u5834\u666f\u3002\u4f8b\u5982\uff0c\u5728\u6578\u64da\u5206\u6790\u4e2d\uff0c\u5e38\u5e38\u9700\u8981\u5f9e\u539f\u59cb\u6587\u672c\u4e2d\u63d0\u53d6\u7279\u5b9a\u6a21\u5f0f\u4e26\u9032\u884c\u66ff\u63db\uff0c\u4ee5\u4fbf\u5f8c\u7e8c\u7684\u6578\u64da\u8655\u7406\u3002<\/p>\n<p>\u82e5\u60f3\u6df1\u5165\u4e86\u89e3 Python \u7684\u5176\u4ed6\u5f37\u5927\u529f\u80fd\uff0c\u5efa\u8b70\u53c3\u8003 [\u9019\u88e1\u7684\u6559\u5b78\u6587\u7ae0](https:\/\/vocus.cc\/article\/5f7a9a7f9d8f4e0014b09b82) \u7372\u53d6\u66f4\u591a\u8cc7\u8a0a\u3002<\/p>\n<p>## \u5e38\u898b\u554f\u984c\u89e3\u7b54\uff08Q&#038;A\uff09<\/p>\n<p>**Q1\uff1a\u5982\u4f55\u6307\u5b9a\u53ea\u66ff\u63db\u90e8\u5206\u5339\u914d\u7684\u5b57\u4e32\uff1f**<br \/>\nA1\uff1a\u53ef\u900f\u904e `count` \u53c3\u6578\u4f86\u9650\u5236\u66ff\u63db\u7684\u6b21\u6578\uff0c\u4f8b\u5982 `re.sub(&#8220;World&#8221;, &#8220;Python&#8221;, s, count=1)` \u53ea\u6703\u66ff\u63db\u7b2c\u4e00\u500b\u5339\u914d\u7684 &#8220;World&#8221;\u3002<\/p>\n<p>**Q2\uff1a\u5982\u4f55\u8655\u7406\u5927\u5c0f\u5beb\u4e0d\u654f\u611f\u7684\u66ff\u63db\uff1f**<br \/>\nA2\uff1a\u53ef\u4ee5\u4f7f\u7528 `flags=re.IGNORECASE` \u4f86\u5ffd\u7565\u5927\u5c0f\u5beb\uff0c\u4f8b\u5982 `re.sub(&#8220;world&#8221;, &#8220;Python&#8221;, s, flags=re.IGNORECASE)`\u3002<\/p>\n<p>**Q3\uff1asub() \u51fd\u6578\u7684\u8fd4\u56de\u503c\u662f\u4ec0\u9ebc\uff1f**<br \/>\nA3\uff1a`sub()` \u51fd\u6578\u6703\u8fd4\u56de\u4e00\u500b\u65b0\u7684\u5b57\u4e32\uff0c\u539f\u59cb\u5b57\u4e32\u4e0d\u6703\u88ab\u6539\u8b8a\u3002\u56e0\u6b64\uff0c\u9700\u5c07\u7d50\u679c\u8ce6\u503c\u7d66\u8b8a\u6578\u4f86\u4fdd\u7559\u66ff\u63db\u7684\u7d50\u679c\u3002<\/p>\n<p>\u9019\u7bc7\u6587\u7ae0\u5e0c\u671b\u80fd\u5e6b\u52a9\u4f60\u66f4\u597d\u5730\u638c\u63e1 Python \u4e2d\u7684 `sub()` \u51fd\u6578\uff0c\u4f7f\u4f60\u5728\u65e5\u5f8c\u7684\u7a0b\u5f0f\u958b\u767c\u4e2d\u80fd\u5920\u66f4\u52a0\u5f97\u5fc3\u61c9\u624b\u3002<br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b78\u7fd2Python\u4e2d\u7684sub()\u51fd\u6578\uff0c\u53ef\u4ee5\u8f15\u9b06\u5730\u5c0d\u5b57\u7b26\u4e32\u9032\u884c\u66ff\u63db\uff0c\u66f4\u5feb\u66f4\u6709\u6548\u5730\u5b8c\u6210\u60a8\u7684\u5de5\u4f5c\u3002\u672c\u6587\u5c07\u4ecb\u7d39sub()\u51fd\u6578\u7684\u7528\u6cd5\uff0c\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528\u5b83\u4f86\u66ff\u63db\u5b57\u7b26\u4e32\u3002<\/p>\n","protected":false},"author":1,"featured_media":2518,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[18],"tags":[15],"class_list":["post-3072","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-python"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2022\/12\/DALL\u00b7E-2022-12-28-14.25.55-\u62f7\u8c9d2.png","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-Ny","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3072","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/comments?post=3072"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3072\/revisions"}],"predecessor-version":[{"id":3073,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3072\/revisions\/3073"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/2518"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=3072"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=3072"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=3072"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}