{"id":1100921,"date":"2025-01-08T15:44:25","date_gmt":"2025-01-08T07:44:25","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1100921.html"},"modified":"2025-01-08T15:44:28","modified_gmt":"2025-01-08T07:44:28","slug":"python%e5%a6%82%e4%bd%95%e6%9b%bf%e6%8d%a2%e5%ad%97%e7%ac%a6%e4%b8%b2%e4%b8%ad%e7%ac%a6%e5%8f%b7-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1100921.html","title":{"rendered":"python\u5982\u4f55\u66ff\u6362\u5b57\u7b26\u4e32\u4e2d\u7b26\u53f7"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25063932\/64a446a6-4ff9-4677-9c6c-411c707ee9f5.webp\" alt=\"python\u5982\u4f55\u66ff\u6362\u5b57\u7b26\u4e32\u4e2d\u7b26\u53f7\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u66ff\u6362\u5b57\u7b26\u4e32\u4e2d\u7684\u7b26\u53f7\u53ef\u4ee5\u4f7f\u7528replace()\u65b9\u6cd5\u3001translate()\u65b9\u6cd5\u3001re.sub()\u51fd\u6570\uff0c\u8fd9\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u9ad8\u6548\u5730\u5b9e\u73b0\u7b26\u53f7\u66ff\u6362\u3002<\/strong>\u5176\u4e2d\uff0creplace()\u65b9\u6cd5\u9002\u7528\u4e8e\u7b80\u5355\u7684\u5b57\u7b26\u4e32\u66ff\u6362\uff0c\u800ctranslate()\u65b9\u6cd5\u548cre.sub()\u51fd\u6570\u5219\u9002\u7528\u4e8e\u66f4\u52a0\u590d\u6742\u7684\u7b26\u53f7\u66ff\u6362\u9700\u6c42\u3002\u4f7f\u7528replace()\u65b9\u6cd5\u66ff\u6362\u7b26\u53f7\u662f\u6700\u7b80\u5355\u4e14\u9ad8\u6548\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001REPLACE()\u65b9\u6cd5<\/p>\n<\/p>\n<p><p>replace()\u65b9\u6cd5\u662fPython\u5185\u7f6e\u5b57\u7b26\u4e32\u65b9\u6cd5\u4e4b\u4e00\uff0c\u7528\u4e8e\u66ff\u6362\u5b57\u7b26\u4e32\u4e2d\u7684\u67d0\u4e2a\u5b50\u5b57\u7b26\u4e32\u3002\u5176\u57fa\u672c\u8bed\u6cd5\u4e3a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">str.replace(old, new[, count])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li><code>old<\/code>\uff1a\u8981\u88ab\u66ff\u6362\u7684\u5b50\u5b57\u7b26\u4e32<\/li>\n<li><code>new<\/code>\uff1a\u66ff\u6362\u540e\u7684\u5b50\u5b57\u7b26\u4e32<\/li>\n<li><code>count<\/code>\uff1a\u53ef\u9009\u53c2\u6570\uff0c\u6307\u660e\u66ff\u6362\u7684\u6b21\u6570\uff08\u9ed8\u8ba4\u66ff\u6362\u6240\u6709\u5339\u914d\u7684\u5b50\u5b57\u7b26\u4e32\uff09<\/li>\n<\/ul>\n<p><p>\u4f8b\u5982\uff0c\u6211\u4eec\u60f3\u8981\u5c06\u5b57\u7b26\u4e32\u4e2d\u7684\u6240\u6709\u9017\u53f7\u66ff\u6362\u4e3a\u53e5\u53f7\uff0c\u53ef\u4ee5\u8fd9\u6837\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;Hello, world! Python is great.&quot;<\/p>\n<p>new_text = text.replace(&quot;,&quot;, &quot;.&quot;)<\/p>\n<p>print(new_text)  # \u8f93\u51fa\uff1aHello. world! Python is great.<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u7b80\u5355\u76f4\u89c2\uff0c\u4f46\u5b83\u53ea\u80fd\u4e00\u6b21\u66ff\u6362\u4e00\u79cd\u5b50\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001TRANSLATE()\u65b9\u6cd5<\/p>\n<\/p>\n<p><p>translate()\u65b9\u6cd5\u7528\u4e8e\u5bf9\u5b57\u7b26\u4e32\u4e2d\u7684\u5b57\u7b26\u8fdb\u884c\u8f6c\u6362\u3002\u5176\u57fa\u672c\u8bed\u6cd5\u4e3a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">str.translate(table)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5176\u4e2d\uff0c<code>table<\/code>\u662f\u4e00\u4e2a\u8f6c\u6362\u8868\uff0c\u901a\u5e38\u7531str.maketrans()\u65b9\u6cd5\u521b\u5efa\u3002str.maketrans()\u65b9\u6cd5\u7528\u4e8e\u521b\u5efa\u5b57\u7b26\u6620\u5c04\u8f6c\u6362\u8868\uff0c\u5176\u57fa\u672c\u8bed\u6cd5\u4e3a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">str.maketrans(x[, y[, z]])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li><code>x<\/code>\uff1a\u9700\u8981\u66ff\u6362\u7684\u5b57\u7b26\u5e8f\u5217<\/li>\n<li><code>y<\/code>\uff1a\u66ff\u6362\u540e\u7684\u5b57\u7b26\u5e8f\u5217<\/li>\n<li><code>z<\/code>\uff1a\u53ef\u9009\u53c2\u6570\uff0c\u9700\u8981\u5220\u9664\u7684\u5b57\u7b26\u5e8f\u5217<\/li>\n<\/ul>\n<p><p>\u4f8b\u5982\uff0c\u6211\u4eec\u60f3\u8981\u5c06\u5b57\u7b26\u4e32\u4e2d\u7684\u9017\u53f7\u66ff\u6362\u4e3a\u53e5\u53f7\uff0c\u5e76\u5220\u9664\u611f\u53f9\u53f7\uff0c\u53ef\u4ee5\u8fd9\u6837\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;Hello, world! Python is great.&quot;<\/p>\n<p>table = str.maketrans(&quot;,&quot;, &quot;.&quot;, &quot;!&quot;)<\/p>\n<p>new_text = text.translate(table)<\/p>\n<p>print(new_text)  # \u8f93\u51fa\uff1aHello. world Python is great.<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u4e00\u6b21\u6027\u66ff\u6362\u591a\u4e2a\u5b57\u7b26\uff0c\u975e\u5e38\u9ad8\u6548\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001RE.SUB()\u51fd\u6570<\/p>\n<\/p>\n<p><p>re.sub()\u51fd\u6570\u662fPython\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5757\uff08re\uff09\u4e2d\u7684\u4e00\u4e2a\u65b9\u6cd5\uff0c\u7528\u4e8e\u66ff\u6362\u5b57\u7b26\u4e32\u4e2d\u7684\u5339\u914d\u9879\u3002\u5176\u57fa\u672c\u8bed\u6cd5\u4e3a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">re.sub(pattern, repl, string, count=0, flags=0)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li><code>pattern<\/code>\uff1a\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f\uff0c\u7528\u4e8e\u5339\u914d\u9700\u8981\u66ff\u6362\u7684\u5b50\u5b57\u7b26\u4e32<\/li>\n<li><code>repl<\/code>\uff1a\u66ff\u6362\u540e\u7684\u5b57\u7b26\u4e32<\/li>\n<li><code>string<\/code>\uff1a\u9700\u8981\u8fdb\u884c\u66ff\u6362\u64cd\u4f5c\u7684\u5b57\u7b26\u4e32<\/li>\n<li><code>count<\/code>\uff1a\u53ef\u9009\u53c2\u6570\uff0c\u6307\u660e\u66ff\u6362\u7684\u6b21\u6570\uff08\u9ed8\u8ba4\u66ff\u6362\u6240\u6709\u5339\u914d\u9879\uff09<\/li>\n<li><code>flags<\/code>\uff1a\u53ef\u9009\u53c2\u6570\uff0c\u6307\u660e\u5339\u914d\u6a21\u5f0f<\/li>\n<\/ul>\n<p><p>\u4f8b\u5982\uff0c\u6211\u4eec\u60f3\u8981\u5c06\u5b57\u7b26\u4e32\u4e2d\u7684\u6240\u6709\u7b26\u53f7\u66ff\u6362\u4e3a\u7a7a\u683c\uff0c\u53ef\u4ee5\u8fd9\u6837\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>text = &quot;Hello, world! Python is great.&quot;<\/p>\n<p>new_text = re.sub(r&#39;[^\\w\\s]&#39;, &#39; &#39;, text)<\/p>\n<p>print(new_text)  # \u8f93\u51fa\uff1aHello  world  Python is great <\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u590d\u6742\u7684\u7b26\u53f7\u66ff\u6362\u9700\u6c42\uff0c\u4f8b\u5982\u66ff\u6362\u591a\u79cd\u7b26\u53f7\u6216\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u8fdb\u884c\u6a21\u5f0f\u5339\u914d\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u591a\u4e2a\u7b26\u53f7\u66ff\u6362<\/p>\n<\/p>\n<p><p>\u6709\u65f6\u5019\uff0c\u6211\u4eec\u9700\u8981\u540c\u65f6\u66ff\u6362\u5b57\u7b26\u4e32\u4e2d\u7684\u591a\u79cd\u7b26\u53f7\uff0c\u53ef\u4ee5\u7ed3\u5408\u4e0a\u8ff0\u65b9\u6cd5\u5b9e\u73b0\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u60f3\u8981\u5c06\u5b57\u7b26\u4e32\u4e2d\u7684\u9017\u53f7\u66ff\u6362\u4e3a\u53e5\u53f7\uff0c\u5e76\u5220\u9664\u611f\u53f9\u53f7\u548c\u95ee\u53f7\uff0c\u53ef\u4ee5\u8fd9\u6837\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;Hello, world! How are you? Python is great.&quot;<\/p>\n<p>table = str.maketrans(&quot;,&quot;, &quot;.&quot;, &quot;!?&quot;)<\/p>\n<p>new_text = text.translate(table)<\/p>\n<p>print(new_text)  # \u8f93\u51fa\uff1aHello. world How are you Python is great<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u9ad8\u6548\u5730\u66ff\u6362\u591a\u4e2a\u7b26\u53f7\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u9ad8\u7ea7\u5e94\u7528<\/p>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\uff08Regular Expression\uff0c\u7b80\u79f0regex\uff09\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u5b57\u7b26\u4e32\u5339\u914d\u5de5\u5177\uff0c\u53ef\u4ee5\u7528\u4e8e\u590d\u6742\u7684\u5b57\u7b26\u4e32\u66ff\u6362\u9700\u6c42\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u60f3\u8981\u5c06\u5b57\u7b26\u4e32\u4e2d\u7684\u6240\u6709\u975e\u5b57\u6bcd\u5b57\u7b26\u66ff\u6362\u4e3a\u7a7a\u683c\uff0c\u53ef\u4ee5\u8fd9\u6837\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>text = &quot;Hello, world! Python3.8 is great.&quot;<\/p>\n<p>new_text = re.sub(r&#39;[^a-zA-Z]&#39;, &#39; &#39;, text)<\/p>\n<p>print(new_text)  # \u8f93\u51fa\uff1aHello  world  Python    is great <\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u7075\u6d3b\u5730\u5904\u7406\u5404\u79cd\u590d\u6742\u7684\u7b26\u53f7\u66ff\u6362\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><p>\u516d\u3001\u4f7f\u7528\u81ea\u5b9a\u4e49\u51fd\u6570\u8fdb\u884c\u66ff\u6362<\/p>\n<\/p>\n<p><p>\u6709\u65f6\u5019\uff0c\u6211\u4eec\u9700\u8981\u6839\u636e\u7279\u5b9a\u89c4\u5219\u8fdb\u884c\u7b26\u53f7\u66ff\u6362\uff0c\u53ef\u4ee5\u4f7f\u7528\u81ea\u5b9a\u4e49\u51fd\u6570\u5b9e\u73b0\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u60f3\u8981\u5c06\u5b57\u7b26\u4e32\u4e2d\u7684\u6240\u6709\u7b26\u53f7\u66ff\u6362\u4e3a\u5b83\u4eec\u7684ASCII\u7801\uff0c\u53ef\u4ee5\u8fd9\u6837\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>def replace_with_ascii(match):<\/p>\n<p>    return str(ord(match.group(0)))<\/p>\n<p>text = &quot;Hello, world! Python is great.&quot;<\/p>\n<p>new_text = re.sub(r&#39;[^\\w\\s]&#39;, replace_with_ascii, text)<\/p>\n<p>print(new_text)  # \u8f93\u51fa\uff1aHello44 world33 Python is great46<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u6839\u636e\u9700\u8981\u7075\u6d3b\u5b9a\u4e49\u66ff\u6362\u89c4\u5219\u3002<\/p>\n<\/p>\n<p><p>\u4e03\u3001\u66ff\u6362\u5e26\u6709\u7279\u5b9a\u6a21\u5f0f\u7684\u7b26\u53f7<\/p>\n<\/p>\n<p><p>\u6709\u65f6\u5019\uff0c\u6211\u4eec\u9700\u8981\u66ff\u6362\u5e26\u6709\u7279\u5b9a\u6a21\u5f0f\u7684\u7b26\u53f7\uff0c\u53ef\u4ee5\u7ed3\u5408\u6b63\u5219\u8868\u8fbe\u5f0f\u548c\u81ea\u5b9a\u4e49\u51fd\u6570\u5b9e\u73b0\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u60f3\u8981\u5c06\u5b57\u7b26\u4e32\u4e2d\u6240\u6709\u4ee5\u5143\u97f3\u5b57\u6bcd\u5f00\u5934\u7684\u5355\u8bcd\u66ff\u6362\u4e3a\u661f\u53f7\uff0c\u53ef\u4ee5\u8fd9\u6837\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>def replace_with_star(match):<\/p>\n<p>    return &#39;*&#39; * len(match.group(0))<\/p>\n<p>text = &quot;An apple a day keeps the doctor away.&quot;<\/p>\n<p>new_text = re.sub(r&#39;\\b[aeiouAEIOU]\\w*\\b&#39;, replace_with_star, text)<\/p>\n<p>print(new_text)  # \u8f93\u51fa\uff1a* apple * day keeps the doctor away.<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u7075\u6d3b\u5904\u7406\u5e26\u6709\u7279\u5b9a\u6a21\u5f0f\u7684\u7b26\u53f7\u66ff\u6362\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><p>\u516b\u3001\u5904\u7406Unicode\u5b57\u7b26\u7684\u66ff\u6362<\/p>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5305\u542bUnicode\u5b57\u7b26\u7684\u5b57\u7b26\u4e32\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u4e2d\u7684Unicode\u5b57\u7b26\u7c7b\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u60f3\u8981\u5c06\u5b57\u7b26\u4e32\u4e2d\u7684\u6240\u6709\u975e\u5b57\u6bcd\u5b57\u7b26\u66ff\u6362\u4e3a\u7a7a\u683c\uff0c\u53ef\u4ee5\u8fd9\u6837\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>text = &quot;Hello, \u4e16\u754c! Python is great.&quot;<\/p>\n<p>new_text = re.sub(r&#39;[^\\w\\s]&#39;, &#39; &#39;, text, flags=re.UNICODE)<\/p>\n<p>print(new_text)  # \u8f93\u51fa\uff1aHello  \u4e16\u754c  Python is great <\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u6709\u6548\u5904\u7406\u5305\u542bUnicode\u5b57\u7b26\u7684\u5b57\u7b26\u4e32\u66ff\u6362\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><p>\u7efc\u4e0a\u6240\u8ff0\uff0cPython\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u7528\u4e8e\u66ff\u6362\u5b57\u7b26\u4e32\u4e2d\u7684\u7b26\u53f7\uff0c\u5305\u62ecreplace()\u65b9\u6cd5\u3001translate()\u65b9\u6cd5\u3001re.sub()\u51fd\u6570\u7b49\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u9ad8\u6548\u5730\u5b9e\u73b0\u7b26\u53f7\u66ff\u6362\u64cd\u4f5c\u3002\u65e0\u8bba\u662f\u7b80\u5355\u7684\u5355\u4e2a\u7b26\u53f7\u66ff\u6362\uff0c\u8fd8\u662f\u590d\u6742\u7684\u591a\u7b26\u53f7\u66ff\u6362\uff0c\u751a\u81f3\u662f\u6839\u636e\u7279\u5b9a\u6a21\u5f0f\u6216\u89c4\u5219\u8fdb\u884c\u66ff\u6362\uff0cPython\u90fd\u80fd\u63d0\u4f9b\u5f3a\u5927\u7684\u652f\u6301\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u66ff\u6362\u7279\u5b9a\u5b57\u7b26\u6216\u7b26\u53f7\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u7684<code>replace()<\/code>\u65b9\u6cd5\u6765\u66ff\u6362\u7279\u5b9a\u7684\u5b57\u7b26\u6216\u7b26\u53f7\u3002\u8be5\u65b9\u6cd5\u63a5\u53d7\u4e24\u4e2a\u53c2\u6570\uff0c\u7b2c\u4e00\u4e2a\u662f\u8981\u66ff\u6362\u7684\u5b57\u7b26\uff0c\u7b2c\u4e8c\u4e2a\u662f\u66ff\u6362\u6210\u7684\u5b57\u7b26\u3002\u4f8b\u5982\uff0c\u5982\u679c\u60f3\u5c06\u5b57\u7b26\u4e32\u4e2d\u7684\u9017\u53f7\u66ff\u6362\u6210\u53e5\u53f7\uff0c\u53ef\u4ee5\u8fd9\u6837\u5199\uff1a<\/p>\n<pre><code class=\"language-python\">text = &quot;Hello, world!&quot;\nnew_text = text.replace(&quot;,&quot;, &quot;.&quot;)\nprint(new_text)  # \u8f93\u51fa: Hello. world!\n<\/code><\/pre>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u66ff\u6362\u5b57\u7b26\u4e32\u4e2d\u7684\u7b26\u53f7\uff1f<\/strong><br \/>\u662f\u7684\uff0cPython\u7684<code>re<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u529f\u80fd\uff0c\u53ef\u4ee5\u7528\u4e8e\u66ff\u6362\u5b57\u7b26\u4e32\u4e2d\u7684\u7b26\u53f7\u3002\u4f7f\u7528<code>re.sub()<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u6839\u636e\u6307\u5b9a\u7684\u6a21\u5f0f\u8fdb\u884c\u66ff\u6362\u3002\u4f8b\u5982\uff0c\u4ee5\u4e0b\u4ee3\u7801\u5c06\u6240\u6709\u975e\u5b57\u6bcd\u5b57\u7b26\u66ff\u6362\u4e3a\u7a7a\u683c\uff1a<\/p>\n<pre><code class=\"language-python\">import re\ntext = &quot;Hello, world! 123&quot;\nnew_text = re.sub(r&#39;[^a-zA-Z]&#39;, &#39; &#39;, text)\nprint(new_text)  # \u8f93\u51fa: Hello  world \n<\/code><\/pre>\n<p><strong>\u5728\u66ff\u6362\u5b57\u7b26\u4e32\u65f6\uff0c\u5982\u4f55\u5904\u7406\u5927\u5c0f\u5199\u95ee\u9898\uff1f<\/strong><br \/>\u5982\u679c\u9700\u8981\u5728\u66ff\u6362\u65f6\u8003\u8651\u5927\u5c0f\u5199\uff0c\u53ef\u4ee5\u4f7f\u7528<code>re<\/code>\u6a21\u5757\u4e2d\u7684<code>re.IGNORECASE<\/code>\u6807\u5fd7\u3002\u8fd9\u6837\uff0c\u65e0\u8bba\u539f\u5b57\u7b26\u4e32\u4e2d\u7684\u5b57\u7b26\u662f\u5927\u5199\u8fd8\u662f\u5c0f\u5199\uff0c\u90fd\u4f1a\u88ab\u66ff\u6362\u3002\u4f8b\u5982\uff0c\u4e0b\u9762\u7684\u4ee3\u7801\u5c06\u66ff\u6362\u5b57\u7b26\u4e32\u4e2d\u7684\u6240\u6709\u201chello\u201d\u548c\u201cHello\u201d\uff1a<\/p>\n<pre><code class=\"language-python\">import re\ntext = &quot;Hello, hello, HeLLo!&quot;\nnew_text = re.sub(r&#39;hello&#39;, &#39;hi&#39;, text, flags=re.IGNORECASE)\nprint(new_text)  # \u8f93\u51fa: hi, hi, hi!\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u66ff\u6362\u5b57\u7b26\u4e32\u4e2d\u7684\u7b26\u53f7\u53ef\u4ee5\u4f7f\u7528replace()\u65b9\u6cd5\u3001translate()\u65b9\u6cd5\u3001re.sub( [&hellip;]","protected":false},"author":3,"featured_media":1100928,"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\/1100921"}],"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=1100921"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1100921\/revisions"}],"predecessor-version":[{"id":1100929,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1100921\/revisions\/1100929"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1100928"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1100921"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1100921"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1100921"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}