{"id":6885,"date":"2022-08-07T11:15:29","date_gmt":"2022-08-07T03:15:29","guid":{"rendered":"http:\/\/123.57.164.21\/?p=6885"},"modified":"2022-08-07T11:15:29","modified_gmt":"2022-08-07T03:15:29","slug":"java-replace%e5%92%8creplaceall","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=6885","title":{"rendered":"Java replace\u548creplaceAll"},"content":{"rendered":"\n<p><strong><a rel=\"noreferrer noopener\" target=\"_blank\"><\/a>\u95ee\u9898<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u6700\u8fd1\u5728\u67e5\u7ebf\u4e0a\u95ee\u9898\u7684\u65f6\u5019\uff0c\u53d1\u73b0\u65e5\u5fd7\u91cc\u9762\u633a\u591a\u8fd9\u79cdPatternSyntaxException\u9519\u8bef\u7684\u3002\u4ece\u65e5\u5fd7\u4e2d\u53ef\u4ee5\u6e05\u695a\u7684\u77e5\u9053\uff0c\u51fa\u95ee\u9898\u7684\u5730\u65b9\u5c31\u662f\u8c03\u7528\u4e86String\u7684replaceAll\u65b9\u6cd5\u3002\u7b49\u7b49\uff0c\u8fd9\u4e2a\u4e0d\u5c31\u662f\u4e2a\u666e\u901a\u7684\u66ff\u6362\u65b9\u6cd5\u561b\uff1f\u96be\u9053\u662f\u6211\u7528\u7684\u59ff\u52bf\u4e0d\u5bf9\uff1f\u8bdd\u4e0d\u591a\u8bf4\uff0c\u8d76\u7d27\u770b\u4e00\u6ce2\u6e90\u7801\uff0c\u5c06bug\u4fee\u590d<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-38-1024x294.png\" alt=\"\" class=\"wp-image-6886\" width=\"577\" height=\"166\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-38-1024x294.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-38-300x86.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-38-768x221.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-38-1536x441.png 1536w, https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-38-830x238.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-38-230x66.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-38-350x100.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-38-480x138.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2022\/08\/\u56fe\u7247-38.png 1818w\" sizes=\"(max-width: 577px) 100vw, 577px\" \/><\/figure><\/div>\n\n\n\n<p><strong><a rel=\"noreferrer noopener\" target=\"_blank\"><\/a>\u539f\u56e0\u5206\u6790\u4ee5\u53ca\u89e3\u51b3\u65b9\u6848<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u5728\u5927\u591a\u6570\u4eba\u7684\u8ba4\u77e5\u91cc\uff0creplace\u65b9\u6cd5\u662f\u66ff\u6362\u7b26\u5408\u7684\u5355\u4e2a\u5b57\u7b26\u4e32\uff0c\u800creplaceAll\u662f\u66ff\u6362\u6240\u6709\u7b26\u5408\u7684\u5b57\u7b26\u4e32\u3002\u7784\u4e86\u4e00\u4e0b\u6e90\u7801\uff0c\u771f\u7684\u6253\u8138\u4e86\u3002<\/p>\n\n\n\n<p><a rel=\"noreferrer noopener\" target=\"_blank\"><\/a>\u6e90\u7801\u5206\u6790<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"> \/**\n     * Replaces each substring of this string that matches the literal target\n     * sequence with the specified literal replacement sequence. The\n     * replacement proceeds from the beginning of the string to the end, for\n     * example, replacing \"aa\" with \"b\" in the string \"aaa\" will result in\n     * \"ba\" rather than \"ab\".\n      \u7ffb\u8bd1\uff1a\u7528\u6307\u5b9a\u7684\u6587\u5b57\u5e8f\u5217\u66ff\u6362\u4e0e\u76ee\u6807\u6587\u5b57\u5e8f\u5217\u4e2d\u5339\u914d\u7684\u6bcf\u4e2a\u5b50\u5b57\u7b26\u4e32\n     *\/\n    public String replace(CharSequence target, CharSequence replacement) {\n        return Pattern.compile(target.toString(), Pattern.LITERAL).matcher(\n                this).replaceAll(Matcher.quoteReplacement(replacement.toString()));\n    }\n\n\n    \/**\n     * Replaces each substring of this string that matches the given &lt;a\n     * href=\"..\/util\/regex\/Pattern.html#sum\">regular expression&lt;\/a> with the\n     * given replacement.\n      \u7ffb\u8bd1\uff1a\u5c06\u76ee\u6807\u6587\u5b57\u5e8f\u5217\u4e2d\u4e0e\u7ed9\u5b9a\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u7684\u6bcf\u4e2a\u5b57\u7b26\u4e32\u66ff\u6362\n     *\/\n    public String replaceAll(String regex, String replacement) {\n        return Pattern.compile(regex).matcher(this).replaceAll(replacement);\n    }\n<\/pre>\n\n\n\n<p>replace\u548creplaceAll\u662fJAVA\u4e2d\u5e38\u7528\u7684\u66ff\u6362\u5b57\u7b26\u7684\u65b9\u6cd5<\/p>\n\n\n\n<ul><li><strong>public String replace(char oldChar, char newChar)<\/strong>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u5728\u5b57\u7b26\u4e32\u4e2d\u7528newChar\u5b57\u7b26\u66ff\u4ee3oldChar\u5b57\u7b26\uff0c\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u5b57\u7b26\u4e32<\/li><li>public String<strong>\u00a0replaceAl<\/strong>l(String regex,String replacement)\u4f7f\u7528\u7ed9\u5b9a\u7684 replacement \u5b57\u7b26\u4e32\u66ff\u6362\u6b64\u5b57\u7b26\u4e32\u5339\u914d\u7ed9\u5b9a\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u6bcf\u4e2a\u5b50\u5b57\u7b26\u4e32\u3002<\/li><\/ul>\n\n\n\n<p><strong>\u533a\u522b\uff1a<\/strong><\/p>\n\n\n\n<ul><li><strong>replace\u7684\u53c2\u6570\u662fchar\u548cCharSequence\uff0c\u5373\u53ef\u4ee5\u652f\u6301\u5b57\u7b26\u7684\u66ff\u6362<\/strong>\uff0c\u4e5f\u652f\u6301\u5b57\u7b26\u4e32\u7684\u66ff\u6362(CharSequence\u5373\u5b57\u7b26\u4e32\u5e8f\u5217\u7684\u610f\u601d,\u8bf4\u767d\u4e86\u4e5f\u5c31\u662f\u5b57\u7b26\u4e32)\uff1b<\/li><\/ul>\n\n\n\n<ul><li><strong>replaceAll\u7684\u53c2\u6570\u662fregex\uff0c\u5373\u57fa\u4e8e\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u66ff\u6362\uff0c<\/strong>\u6bd4\u5982\uff0c\u53ef\u4ee5\u901a\u8fc7replaceAll(&#8220;<a>\\\\d<\/a>&#8220;, &#8220;*&#8221;)\u628a\u4e00\u4e2a\u5b57\u7b26\u4e32\u6240\u6709\u7684\u6570\u5b57\u5b57\u7b26\u90fd\u6362\u6210\u661f\u53f7;<\/li><li><\/li><\/ul>\n\n\n\n<p><strong>\u76f8\u540c\u70b9\uff1a<\/strong><\/p>\n\n\n\n<p>\u90fd\u662f\u5168\u90e8\u66ff\u6362\uff0c\u5e95\u5c42\u90fd\u662f\u8c03\u7528\u4e86Matcher.replaceAll()\u65b9\u6cd5\uff0c\u5373\u628a\u6e90\u5b57\u7b26\u4e32\u4e2d\u7684\u67d0\u4e00\u5b57\u7b26\u6216\u5b57\u7b26\u4e32\u5168\u90e8\u6362\u6210\u6307\u5b9a\u7684\u5b57\u7b26\u6216\u5b57\u7b26\u4e32\uff0c\u5982\u679c\u53ea\u60f3\u66ff\u6362\u7b2c\u4e00\u6b21\u51fa\u73b0\u7684\uff0c\u53ef\u4ee5\u4f7f\u7528replaceFirst()\uff0c\u8fd9\u4e2a\u65b9\u6cd5\u4e5f\u662f\u57fa\u4e8e\u89c4\u5219\u8868\u8fbe\u5f0f\u7684\u66ff\u6362\uff0c\u4f46\u4e0ereplaceAll()\u4e0d\u540c\u7684\u662f\uff0c\u53ea\u66ff\u6362\u7b2c\u4e00\u6b21\u51fa\u73b0\u7684\u5b57\u7b26\u4e32\uff1b<\/p>\n\n\n\n<p>\u53e6\u5916\uff0c\u5982\u679creplaceAll()\u548creplaceFirst()\u6240\u7528\u7684\u53c2\u6570\u636e\u4e0d\u662f\u57fa\u4e8e\u89c4\u5219\u8868\u8fbe\u5f0f\u7684\uff0c\u5219\u4e0ereplace()\u66ff\u6362\u5b57\u7b26\u4e32\u7684\u6548\u679c\u662f\u4e00\u6837\u7684\uff0c\u5373\u8fd9\u4e24\u8005\u4e5f\u652f\u6301\u5b57\u7b26\u4e32\u7684\u64cd\u4f5c\uff1b<\/p>\n\n\n\n<p>\u8fd8\u6709\u4e00\u70b9\u6ce8\u610f:\uff1a\u6267\u884c\u4e86\u66ff\u6362\u64cd\u4f5c\u540e,\u6e90\u5b57\u7b26\u4e32\u7684\u5185\u5bb9\u662f\u6ca1\u6709\u53d1\u751f\u6539\u53d8\u7684\u3002<\/p>\n\n\n\n<h5 class=\"wp-block-heading\"><a target=\"_blank\" rel=\"noreferrer noopener\"><\/a><\/h5>\n\n\n\n<p><strong><a rel=\"noreferrer noopener\" target=\"_blank\"><\/a>\u539f\u56e0<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u7531\u4e8ereplacAll()\u65b9\u6cd5\uff0c\u88ab\u66ff\u6362\u7684\u53c2\u6570\u652f\u6301\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u800c\u6211\u4f20\u5165\u7684\u503c\u5b58\u5728\u7279\u6b8a\u5b57\u7b26\uff0c\u6ca1\u6709\u8fdb\u884c\u8f6c\u4e49\uff0c\u5bfc\u81f4\u89e3\u6790\u5931\u8d25\u3002<\/p>\n\n\n\n<p><strong><a rel=\"noreferrer noopener\" target=\"_blank\"><\/a>\u89e3\u51b3\u65b9\u6848<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u4e1a\u52a1\u9700\u6c42\u662f\u5c06\u6307\u5b9a\u7684\u5b57\u7b26\u4e32\u76f4\u63a5\u5168\u90e8\u66ff\u6362\u6389\uff0c\u6240\u4ee5\u53ea\u9700\u5c06replaceAll()\u65b9\u6cd5\u66f4\u6362\u4e3areplace()\u65b9\u6cd5\u5373\u53ef\uff0c\u89e3\u51b3\u95ee\u9898\u3002<\/p>\n\n\n\n<p><strong><a rel=\"noreferrer noopener\" target=\"_blank\"><\/a>\u4ee3\u7801\u8bba\u8bc1<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u770b\u4e00\u4e2a\u4f8b\u5b50<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">        String word1 = \"test.test.test\";\n        String word2 = \"test.test.test\";\n        String word3 = \"test.test.test\";\n\n        String word11 = word1.replace(\".\",\"@\");  \/\/test@test@test\n        String word22 = word2.replaceAll(\".\",\"@\"); \/\/@@@@@@@@@@@@@@\n        String word33 = word3.replaceFirst(\".\",\"@\");\/\/@est.test.test\n\n        System.out.println(\"word11: \" + word11 + \";word22:\" + word22 + \";word33: \" + word33);\n        System.out.println(\"word1: \" + word1 + \";word2:\" + word2 + \";word3: \" + word3);\n\nword11: test@test@test;word22:@@@@@@@@@@@@@@;word33: @est.test.test\nword1: test.test.test;word2:test.test.test;word3: test.test.test\n<\/pre>\n\n\n\n<p>\u7531\u4e8e&#8221;.&#8221;\u5c5e\u4e8e\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u7b26\u53f7\uff08\u5339\u914d\u9664\u6362\u884c\u7b26 \\n \u4e4b\u5916\u7684\u4efb\u4f55\u5355\u5b57\u7b26\uff09\uff0c\u6240\u4ee5replaceAll()\u6267\u884c\u6b63\u5219\u66ff\u6362\uff1b\u6b64\u5916\uff0c\u6211\u4eec\u53ef\u4ee5\u6ce8\u610f\u5230,replaceFirst()\u8fd9\u4e2a\u65b9\u6cd5\uff0c\u5176\u5b9e\u8fd9\u4e2a\u65b9\u6cd5\u8ddfreplaceAll()\u5b9e\u73b0\u662f\u4e00\u6837\u7684\uff0c\u6267\u884c\u6b63\u5219\u66ff\u6362\uff0c\u53ea\u662f\u8be5\u65b9\u6cd5\u53ea\u66ff\u6362\u7b26\u5408\u6761\u4ef6\u7684\u7b2c\u4e00\u4e2a\u5b57\u7b26\u6216\u8005\u5b57\u7b26\u4e32<\/p>\n\n\n\n<p><strong><a rel=\"noreferrer noopener\" target=\"_blank\"><\/a>\u603b\u7ed3<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<ul><li>replace()\u65b9\u6cd5\u662f\u5c06\u76ee\u6807\u5b57\u7b26\u4e32\u4e2d<strong>\u5b8c\u5168\u5339\u914d<\/strong>\u7684\u6307\u5b9a\u7684\u5b57\u7b26\u6216\u8005\u5b57\u7b26\u4e32<strong>\u5168\u90e8<\/strong>\u66ff\u6362<\/li><li>replaceAll()\u548creplaceFirst()\u65b9\u6cd5\uff0c\u5219\u662f\u57fa\u4e8e\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u66ff\u6362\uff0c\u6bd4\u5982\uff0c\u901a\u8fc7replace(&#8220;\\d&#8221;,&#8221;@&#8221;)\u662f\u628a\u4e00\u4e2a\u5b57\u7b26\u4e32\u4e2d\u7684\u6240\u6709\u6570\u5b57\u66ff\u6362\u4e3a@<\/li><li>\u4ece\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6267\u884c\u66ff\u6362\u64cd\u4f5c\u4e4b\u540e\uff0c\u5176\u5b9e\u662f\u8fd4\u56de\u4e00\u4e2a\u65b0\u5bf9\u8c61\uff0c\u6e90\u5b57\u7b26\u4e32\u7684\u5185\u5bb9\u5219\u662f\u4e0d\u53d8\u7684\u3002<\/li><\/ul>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u95ee\u9898 \u6700\u8fd1\u5728\u67e5\u7ebf\u4e0a\u95ee\u9898\u7684\u65f6\u5019\uff0c\u53d1\u73b0\u65e5\u5fd7\u91cc\u9762\u633a\u591a\u8fd9\u79cdPatternSyntaxException\u9519\u8bef\u7684\u3002\u4ece\u65e5\u5fd7\u4e2d [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[26],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/6885"}],"collection":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6885"}],"version-history":[{"count":1,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/6885\/revisions"}],"predecessor-version":[{"id":6887,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/6885\/revisions\/6887"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6885"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6885"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6885"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}