{"id":3526,"date":"2025-05-31T14:31:18","date_gmt":"2025-06-04T07:18:18","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=3526"},"modified":"2025-06-04T14:31:18","modified_gmt":"2025-06-04T07:18:18","slug":"%e4%ba%86%e8%a7%a3python%e4%b8%ad%e7%9a%84sub%e5%87%bd%e6%95%b8%ef%bc%9a%e6%9b%bf%e6%8f%9b%e5%ad%97%e4%b8%b2%e4%b8%ad%e7%9a%84%e6%96%87%e5%ad%97","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%ef%bc%9a%e6%9b%bf%e6%8f%9b%e5%ad%97%e4%b8%b2%e4%b8%ad%e7%9a%84%e6%96%87%e5%ad%97\/","title":{"rendered":"2025 \u5e74 Python sub() \u51fd\u6578\u6df1\u5165\u89e3\u6790\uff1a\u5b57\u4e32\u66ff\u63db\u7684\u6700\u4f73\u5be6\u8e10"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, sub(), \u5b57\u4e32\u66ff\u63db, \u6b63\u5247\u8868\u9054\u5f0f\"><\/p>\n<h1>\u6df1\u5165\u4e86\u89e3 Python \u4e2d\u7684 sub() \u51fd\u6578<\/h1>\n<p>\u5728 Python \u7de8\u7a0b\u4e2d\uff0c`sub()` \u51fd\u6578\u662f\u4e00\u500b\u5f37\u5927\u4e14\u9748\u6d3b\u7684\u5de5\u5177\uff0c\u5c08\u9580\u7528\u65bc\u5b57\u4e32\u7684\u66ff\u63db\u3002\u5b83\u4e0d\u50c5\u53ef\u4ee5\u66ff\u63db\u6307\u5b9a\u7684\u5b57\u4e32\uff0c\u9084\u80fd\u5229\u7528\u6b63\u5247\u8868\u9054\u5f0f\u9032\u884c\u66f4\u8907\u96dc\u7684\u5339\u914d\u3002\u672c\u6587\u5c07\u4ecb\u7d39\u5176\u6700\u65b0\u7684\u7528\u6cd5\u4ee5\u53ca\u6700\u4f73\u5be6\u8e10\uff0c\u5e6b\u52a9\u4f60\u5145\u5206\u767c\u63ee\u6b64\u51fd\u6578\u7684\u6f5b\u529b\u3002<\/p>\n<h2>sub() \u51fd\u5f0f\u7684\u57fa\u672c\u7528\u6cd5<\/h2>\n<p>`sub()` \u51fd\u6578\u7684\u57fa\u672c\u8a9e\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre class=\"brush: python\">\nre.sub(pattern, repl, string, count=0, flags=0)\n<\/pre>\n<p>&#8211; **pattern**: \u8981\u641c\u5c0b\u7684\u5b57\u4e32\u6216\u6b63\u5247\u8868\u9054\u5f0f\u3002<br \/>\n&#8211; **repl**: \u8981\u66ff\u63db\u7684\u5b57\u4e32\u6216\u51fd\u5f0f\u3002<br \/>\n&#8211; **string**: \u88ab\u641c\u5c0b\u7684\u5b57\u4e32\u3002<br \/>\n&#8211; **count**: \u53ef\u9078\uff0c\u6307\u5b9a\u66ff\u63db\u7684\u6b21\u6578\uff0c\u9810\u8a2d\u70ba 0\uff0c\u8868\u793a\u66ff\u63db\u6240\u6709\u5339\u914d\u9805\u3002<br \/>\n&#8211; **flags**: \u53ef\u9078\uff0c\u6307\u5b9a\u641c\u5c0b\u7684\u65b9\u5f0f\uff0c\u5982\u662f\u5426\u5340\u5206\u5927\u5c0f\u5beb\u3002<\/p>\n<h2>\u5be6\u4f5c\u7bc4\u4f8b<\/h2>\n<p>\u5047\u8a2d\u6211\u5011\u6709\u4e00\u500b\u5b57\u4e32\uff1a<\/p>\n<pre class=\"brush: python\">\ns = 'Hello, world!'\n<\/pre>\n<p>\u6211\u5011\u53ef\u4ee5\u4f7f\u7528 `sub()` \u51fd\u6578\u5c07\u5176\u4e2d\u7684 &#8216;world&#8217; \u66ff\u63db\u70ba &#8216;Python&#8217;\uff1a<\/p>\n<pre class=\"brush: python\">\nimport re\n\ns = re.sub('world', 'Python', s)\nprint(s)\n<\/pre>\n<p>\u57f7\u884c\u7d50\u679c\uff1a<\/p>\n<pre class=\"brush: python\">\nHello, Python!\n<\/pre>\n<h2>\u4f7f\u7528\u51fd\u5f0f\u4f5c\u70ba\u66ff\u63db\u53c3\u6578<\/h2>\n<p>`sub()` \u51fd\u6578\u9084\u53ef\u4ee5\u63a5\u53d7\u4e00\u500b\u51fd\u5f0f\u4f5c\u70ba\u53c3\u6578\uff0c\u8a72\u51fd\u5f0f\u63a5\u6536\u4e00\u500b\u5339\u914d\u5c0d\u8c61\u4e26\u8fd4\u56de\u4e00\u500b\u66ff\u4ee3\u5b57\u4e32\u3002\u4ee5\u4e0b\u662f\u793a\u7bc4\uff1a<\/p>\n<pre class=\"brush: python\">\nimport re\n\ndef repl(m):\n    return 'Python'\n\ns = re.sub('world', repl, s)\nprint(s)\n<\/pre>\n<p>\u57f7\u884c\u7d50\u679c\uff1a<\/p>\n<pre class=\"brush: python\">\nHello, Python!\n<\/pre>\n<h2>\u5229\u7528\u6b63\u5247\u8868\u9054\u5f0f\u7684\u5f37\u5927\u529f\u80fd<\/h2>\n<p>\u4f60\u4e5f\u53ef\u4ee5\u4f7f\u7528\u6b63\u5247\u8868\u9054\u5f0f\u4f86\u6307\u5b9a\u66f4\u8907\u96dc\u7684\u5339\u914d\u689d\u4ef6\u3002\u4f8b\u5982\uff0c\u5c07\u6240\u6709\u5b57\u6bcd\u66ff\u63db\u70ba &#8216;Python&#8217;\uff1a<\/p>\n<pre class=\"brush: python\">\nimport re\n\ns = 'Hello, world!'\ns = re.sub(r'\\w+', 'Python', s)\nprint(s)\n<\/pre>\n<p>\u57f7\u884c\u7d50\u679c\uff1a<\/p>\n<pre class=\"brush: python\">\nPython, Python!\n<\/pre>\n<h2>\u932f\u8aa4\u6392\u9664\u8207\u6700\u4f73\u5be6\u8e10<\/h2>\n<p>\u5728\u4f7f\u7528 `sub()` \u51fd\u6578\u6642\uff0c\u5e38\u898b\u7684\u932f\u8aa4\u5305\u62ec\uff1a<\/p>\n<p>&#8211; \u5fd8\u8a18\u5c0e\u5165 `re` \u6a21\u7d44\u3002<br \/>\n&#8211; \u4f7f\u7528\u4e0d\u6b63\u78ba\u7684\u6b63\u5247\u8868\u9054\u5f0f\uff0c\u5c0e\u81f4\u7121\u6cd5\u5339\u914d\u9810\u671f\u5b57\u4e32\u3002<br \/>\n&#8211; \u672a\u8003\u616e\u5927\u5c0f\u5beb\u7684\u5dee\u7570\uff0c\u53ef\u80fd\u9700\u8981\u4f7f\u7528 `flags=re.IGNORECASE`\u3002<\/p>\n<p>\u6700\u4f73\u5be6\u8e10\u5efa\u8b70\uff1a<\/p>\n<p>&#8211; \u4f7f\u7528\u6b63\u5247\u8868\u9054\u5f0f\u6642\uff0c\u6e2c\u8a66\u5339\u914d\u6a21\u5f0f\u4ee5\u78ba\u4fdd\u5176\u6b63\u78ba\u6027\u3002<br \/>\n&#8211; \u5728\u9032\u884c\u5927\u91cf\u66ff\u63db\u6642\uff0c\u8003\u616e\u4f7f\u7528`count`\u53c3\u6578\u4f86\u9650\u5236\u66ff\u63db\u6b21\u6578\uff0c\u4ee5\u4fdd\u8b49\u6027\u80fd\u3002<\/p>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>`sub()` \u51fd\u6578\u7684\u61c9\u7528\u7bc4\u570d\u5ee3\u6cdb\uff0c\u5305\u62ec\u4f46\u4e0d\u9650\u65bc\uff1a<\/p>\n<p>&#8211; \u6e05\u7406\u6578\u64da\uff1a\u5c07\u7279\u5b9a\u5b57\u7b26\u6216\u6a21\u5f0f\u5f9e\u6578\u64da\u4e2d\u79fb\u9664\u3002<br \/>\n&#8211; \u683c\u5f0f\u5316\u8f38\u51fa\uff1a\u6839\u64da\u7279\u5b9a\u683c\u5f0f\u66ff\u63db\u5b57\u4e32\uff0c\u5982\u65e5\u671f\u683c\u5f0f\u7684\u7d71\u4e00\u3002<br \/>\n&#8211; \u751f\u6210\u52d5\u614b\u5167\u5bb9\uff1a\u4f7f\u7528\u51fd\u5f0f\u751f\u6210\u66ff\u4ee3\u5b57\u4e32\uff0c\u4f7f\u5167\u5bb9\u66f4\u5177\u8b8a\u5316\u3002<\/p>\n<p>\u5982\u6b32\u4e86\u89e3\u66f4\u591a Python \u7684\u61c9\u7528\u8207\u6559\u5b78\uff0c\u5efa\u8b70\u53c3\u8003 [vocus.cc \u7684 Python \u6559\u5b78](https:\/\/vocus.cc)\uff0c\u9019\u88e1\u6709\u8a31\u591a\u5be6\u7528\u7684\u8cc7\u6e90\u53ef\u4ee5\u5e6b\u52a9\u4f60\u9032\u4e00\u6b65\u5b78\u7fd2\u3002<\/p>\n<h2>\u7e3d\u7d50<\/h2>\n<p>Python \u4e2d\u7684 `sub()` \u51fd\u6578\u662f\u4e00\u500b\u529f\u80fd\u5f37\u5927\u7684\u5b57\u4e32\u66ff\u63db\u5de5\u5177\uff0c\u652f\u6301\u4f7f\u7528\u5b57\u4e32\u6216\u8005\u6b63\u5247\u8868\u9054\u5f0f\u4f86\u5b8c\u6210\u66ff\u63db\u4efb\u52d9\u3002\u7121\u8ad6\u4f60\u662f\u8655\u7406\u7c21\u55ae\u7684\u5b57\u4e32\u66ff\u63db\u9084\u662f\u9032\u884c\u8907\u96dc\u7684\u6587\u672c\u8655\u7406\uff0c`sub()` \u90fd\u80fd\u70ba\u4f60\u63d0\u4f9b\u9748\u6d3b\u7684\u89e3\u6c7a\u65b9\u6848\u3002<\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<p>**Q1: sub() \u51fd\u6578\u53ef\u4ee5\u66ff\u63db\u591a\u500b\u5b57\u4e32\u55ce\uff1f**<br \/>\nA1: \u662f\u7684\uff0c\u4f60\u53ef\u4ee5\u5229\u7528\u6b63\u5247\u8868\u9054\u5f0f\u5339\u914d\u591a\u500b\u5b57\u4e32\uff0c\u6216\u8005\u591a\u6b21\u8abf\u7528 `sub()` \u51fd\u6578\u4f86\u9054\u6210\u3002<\/p>\n<p>**Q2: \u5982\u4f55\u4f7f\u7528 sub() \u51fd\u6578\u66ff\u63db\u5927\u5c0f\u5beb\u5b57\u6bcd\uff1f**<br \/>\nA2: \u4f60\u53ef\u4ee5\u4f7f\u7528 `flags=re.IGNORECASE` \u53c3\u6578\u4f86\u5ffd\u7565\u5927\u5c0f\u5beb\u7684\u5dee\u7570\u3002<\/p>\n<p>**Q3: sub() \u51fd\u6578\u7684\u57f7\u884c\u6548\u7387\u5982\u4f55\uff1f**<br \/>\nA3: `sub()` \u51fd\u6578\u7684\u57f7\u884c\u6548\u7387\u76f8\u5c0d\u8f03\u9ad8\uff0c\u4f46\u5728\u8655\u7406\u5927\u91cf\u6578\u64da\u6642\uff0c\u5efa\u8b70\u4f7f\u7528 `count` \u9650\u5236\u66ff\u63db\u6b21\u6578\uff0c\u4ee5\u63d0\u9ad8\u6027\u80fd\u3002<\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e86\u89e3Python\u4e2d\u7684sub()\u51fd\u6578\uff0c\u5b83\u53ef\u4ee5\u66ff\u63db\u5b57\u4e32\u4e2d\u7684\u6587\u5b57\uff0c\u63d0\u4f9b\u66f4\u591a\u7684\u63a7\u5236\uff0c\u8b93\u4f60\u53ef\u4ee5\u66f4\u6709\u6548\u7387\u5730\u5b8c\u6210\u5b57\u4e32\u66ff\u63db\u7684\u5de5\u4f5c\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-3526","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-US","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3526","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=3526"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3526\/revisions"}],"predecessor-version":[{"id":3527,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3526\/revisions\/3527"}],"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=3526"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=3526"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=3526"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}