{"id":985,"date":"2018-11-22T12:00:14","date_gmt":"2018-11-22T03:00:14","guid":{"rendered":"https:\/\/www.bold.ne.jp\/engineer-club\/?p=985"},"modified":"2025-09-17T13:52:50","modified_gmt":"2025-09-17T04:52:50","slug":"java-string-replaceall","status":"publish","type":"post","link":"https:\/\/www.bold.ne.jp\/engineer-club\/java-string-replaceall","title":{"rendered":"Java\u306eString.replaceAll\u3068\u306f\uff1f\u30b9\u30de\u30fc\u30c8\u306b\u6587\u5b57\u5217\u7f6e\u63db\u3059\u308b\u305f\u3081\u306ereplaceAll\u306e\u4f7f\u3044\u65b9"},"content":{"rendered":"<p>String.replaceAll\u306f\u3001\u6587\u5b57\u5217\u4e2d\u3092\u6b63\u898f\u8868\u73fe\u3067\u30de\u30c3\u30c1\u30f3\u30b0\u3057\u3001\u30de\u30c3\u30c1\u3057\u305f\u90e8\u5206\u3092\u7f6e\u63db\u3059\u308b\u3082\u306e\u3067\u3059\u3002<br \/>\n\u540c\u3058\u3088\u3046\u306a\u52d5\u304d\u3092\u3059\u308b\u3082\u306e\u3068\u3057\u3066\u3001<span>replaceFirst<\/span>\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<p>\u3053\u306e\u8a18\u4e8b\u3067\u306f\u3001<span>replaceAll\/replaceFirst<\/span>\u306e\u4f7f\u3044\u65b9\u3068\u3001\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u306e\u73fe\u5834\u3067\u3088\u304f\u898b\u304b\u3051\u308b\u5fdc\u7528\u4f8b\u3092\u521d\u5fc3\u8005\u5411\u3051\u306b\u304a\u4f1d\u3048\u3057\u307e\u3059\u3002<\/p>\n<p>\u306a\u304a\u3001\u4f3c\u305f\u30e1\u30bd\u30c3\u30c9\u3068\u3057\u3066<span>String.replace<\/span>\u304c\u3042\u308a\u307e\u3059\u3002\u3053\u3061\u3089\u306f\u6b63\u898f\u8868\u73fe\u3067\u306f\u306a\u304f\u56fa\u5b9a\u3055\u308c\u305f\u6587\u5b57\u5217\u3092\u5bfe\u8c61\u306b\u7f6e\u63db\u3057\u307e\u3059\u3002<br \/>\n\u3053\u3061\u3089\u3082\u521d\u5fc3\u8005\u5411\u3051\u306e\u8a18\u4e8b\u304c\u3042\u308a\u307e\u3059\u306e\u3067\u3001\u3088\u308d\u3057\u3051\u308c\u3070\u3054\u89a7\u304f\u3060\u3055\u3044\u3002<\/p>\n<p>\u203b\u3053\u306e\u8a18\u4e8b\u306e\u30b5\u30f3\u30d7\u30eb\u306f\u3001<span>Java 10<\/span>\u306e\u74b0\u5883\u3067\u52d5\u4f5c\u78ba\u8a8d\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<div id=\"toc_container\" class=\"no_bullets\"><p class=\"toc_title\">\u76ee\u6b21<\/p><ul class=\"toc_list\"><li><a href=\"#JavaStringreplaceAll\">\uff11\uff0e\u3010Java\u3011String.replaceAll\u306f\u6b63\u898f\u8868\u73fe\u306e\u30de\u30c3\u30c1\u7b87\u6240\u3092\u7f6e\u63db\u3059\u308b<\/a><ul><li><a href=\"#i\">\uff11-\uff11\uff0e\u4f8b\uff1a\u82f1\u8a9e\u5c0f\u6587\u5b57\u30fb\u6570\u5b57\u3068\u306e\u30de\u30c3\u30c1\u30f3\u30b0<\/a><\/li><li><a href=\"#-Java\">\uff11-\uff12\uff0eJava\u3067\u4f7f\u3048\u308b\u6b63\u898f\u8868\u73fe\u306e\u30d1\u30bf\u30fc\u30f3\u4f8b<\/a><\/li><li><a href=\"#-Patternquote\">\uff11-\uff13\uff0e\u7279\u6b8a\u6587\u5b57\u306e\u7121\u52b9\u5316\u306b\u306f\\\u304bPattern.quote\u3092\u4f7f\u3046<\/a><\/li><\/ul><\/li><li><a href=\"#JavaStringreplaceFirst\">\uff12\uff0e\u3010Java\u3011String.replaceFirst\u306f\u6700\u521d\u306e\u30de\u30c3\u30c1\u7b87\u6240\u3060\u3051\u3092\u7f6e\u63db\u3059\u308b<\/a><\/li><li><a href=\"#StringreplaceAll\">\uff13\uff0e\u3010\u5fdc\u7528\u3011String.replaceAll\u306e\u5fdc\u7528\u4f8b<\/a><ul><li><a href=\"#i-2\">\uff13-\uff11\uff0e\u7a7a\u767d\u306e\u524a\u9664\u3084\u8a70\u3081<\/a><\/li><li><a href=\"#-trim\">\uff13-\uff12\uff0e\u5168\u89d2\u30fb\u534a\u89d2\u7a7a\u767d\u4e21\u5bfe\u5fdctrim<\/a><\/li><li><a href=\"#i-3\">\uff13-\uff13\uff0e\u7279\u5b9a\u6587\u8a00\u306e\u30de\u30b9\u30af\u30fb\u6587\u8a00\u306e\u4fee\u6b63<\/a><\/li><li><a href=\"#i-4\">\uff13-\uff14\uff0e\u6539\u884c\u30b3\u30fc\u30c9\u3084\u533a\u5207\u308a\u6587\u5b57\u306e\u7d71\u4e00<\/a><\/li><li><a href=\"#-HTMLXML\">\uff13-\uff15\uff0eHTML\/XML\u4e2d\u306e\u5c5e\u6027\u5024\u306e\u524a\u9664<\/a><\/li><\/ul><\/li><li><a href=\"#StringreplaceAll-2\">\uff14\uff0e\u3010\u4e2d\u7d1a\u8005\u5411\u3051\u3011String.replaceAll\u306e\u3082\u3046\u5c11\u3057\u9032\u3093\u3060\u8a71\u984c<\/a><ul><li><a href=\"#-012\">\uff14-\uff11\uff0e\u30de\u30c3\u30c1\u3057\u305f\u6587\u5b57\u5217\u306e\u53c2\u7167($0\u3001$1\u3001$2\u2026)<\/a><\/li><li><a href=\"#-PatternMatcher\">\uff14-\uff12\uff0ePattern\u3068Matcher\u3067\u6b63\u898f\u8868\u73fe\u306e\u4e16\u754c\u306b\u98db\u3073\u8fbc\u3082\u3046!<\/a><\/li><\/ul><\/li><li><a href=\"#i-5\">\uff15\uff0e\u307e\u3068\u3081<\/a><\/li><\/ul><\/div>\n<h2><span id=\"JavaStringreplaceAll\">\uff11\uff0e\u3010Java\u3011String.replaceAll\u306f\u6b63\u898f\u8868\u73fe\u306e\u30de\u30c3\u30c1\u7b87\u6240\u3092\u7f6e\u63db\u3059\u308b<\/span><\/h2>\n<p>String.replaceAll\u306f\u3001\u300c\u6b63\u898f\u8868\u73fe\u300d\u304c\u30de\u30c3\u30c1\u3057\u305f\u7b87\u6240\u5168\u3066\u3092\u3001\u6307\u5b9a\u306e\u6587\u5b57\u5217\u306b\u7f6e\u63db\u3057\u305f\u6587\u5b57\u5217\u3092\u623b\u3059\u30e1\u30bd\u30c3\u30c9\u3067\u3059\u3002\u305d\u308c\u4ee5\u5916\u306e\u6a5f\u80fd\u306f<span>String.replace<\/span>\u3068\u540c\u3058\u3067\u3059\u3002<\/p>\n<pre class=\"prettyprint lang:default decode:true\">public String replaceAll(String regex, String replacement)\r\n\tregex - \u3053\u306e\u6587\u5b57\u5217\u3068\u306e\u4e00\u81f4\u3092\u5224\u5b9a\u3059\u308b\u6b63\u898f\u8868\u73fe\r\n\treplacement - \u4e00\u81f4\u3059\u308b\u3082\u306e\u305d\u308c\u305e\u308c\u306b\u7f6e\u304d\u63db\u3048\u3089\u308c\u308b\u6587\u5b57\u5217<\/pre>\n<p>\u6b63\u898f\u8868\u73fe(Regular Expression)\u3068\u306f\u3001\u6587\u5b57\u3084\u6570\u5b57\u306e\u300c\u30d1\u30bf\u30fc\u30f3\u300d\u3092\u6307\u5b9a\u3059\u308b\u3082\u306e\u3067\u3059\u3002replaceAll\u304c\u771f\u4fa1\u3092\u767a\u63ee\u3059\u308b\u306e\u306f\u3001\u7f6e\u63db\u3057\u305f\u3044\u6587\u5b57\u5217\u306b\u4f55\u304b\u3057\u3089\u306e\u660e\u78ba\u306a\u30d1\u30bf\u30fc\u30f3\u304c\u3042\u308b\u6642\u3067\u3059\u3002<\/p>\n<h3><span id=\"i\">\uff11-\uff11\uff0e\u4f8b\uff1a\u82f1\u8a9e\u5c0f\u6587\u5b57\u30fb\u6570\u5b57\u3068\u306e\u30de\u30c3\u30c1\u30f3\u30b0<\/span><\/h3>\n<p>\u4f8b\u3048\u3070\u3001\u6b63\u898f\u8868\u73fe\u306e<span>[a-z]<\/span>\u306f\u82f1\u8a9e\u306e\u5c0f\u6587\u5b57<span>(a<\/span>\u304b\u3089<span>z<\/span>\u306e\u5168\u3066<span>)<\/span>\u306b\u30de\u30c3\u30c1\u3057\u307e\u3059\u3057\u3001<span>[0-9]<\/span>\u306f\u6570\u5b57<span>(0<\/span>\uff5e<span>9<\/span>\u306e\u5168\u3066<span>)<\/span>\u306b\u30de\u30c3\u30c1\u3057\u307e\u3059\u3002\u5b9f\u969b\u306b\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u3057\u3066\u7528\u3044\u307e\u3059\u3002<\/p>\n<pre class=\"prettyprint lang:default decode:true\">String str = \"The quick brown fox jumps over the lazy dog. 1234567890\";\r\n\r\n\/\/ a\u304b\u3089z\u306e\u7bc4\u56f2\u306b\u3042\u308b\u6587\u5b57\u306a\u3089\u4f55\u3067\u3082x\u306b\u7f6e\u63db\u3059\u308b\r\nString replaced1 = str.replaceAll(\"[a-z]\", \"x\");\r\nSystem.out.println(replaced1); \/\/ \u2192 Txx xxxxx xxxxx xxx xxxxx xxxx xxx xxxx xxx. 1234567890\r\n\r\n\/\/ 0\u304b\u30899\u306e\u7bc4\u56f2\u306b\u3042\u308b\u6587\u5b57\u306a\u3089\u4f55\u3067\u3082y\u306b\u7f6e\u63db\u3059\u308b\r\nString replaced2 = str.replaceAll(\"[0-9]\", \"y\");\r\nSystem.out.println(replaced2); \/\/ \u2192 The quick brown fox jumps over the lazy dog. yyyyyyyyyy\r\n\r\n\/\/ a\u304b\u3089z\u3042\u308b\u3044\u306f0\u304b\u30899\u306b\u3042\u308b\u6587\u5b57\u306a\u3089\u4f55\u3067\u3082z\u306b\u7f6e\u63db\u3059\u308b\r\nString replaced3 = str.replaceAll(\"[a-z0-9]\", \"z\");\r\nSystem.out.println(replaced3); \/\/ \u2192 Tzz zzzzz zzzzz zzz zzzzz zzzz zzz zzzz zzz. zzzzzzzzzz<\/pre>\n<p>\u3061\u306a\u307f\u306b\u3001\u540c\u3058\u3088\u3046\u306a\u3053\u3068\u3092<span>String.replace<\/span>\u3067\u884c\u3046\u306b\u306f\u3001\u4f8b\u3048\u3070\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u3057\u306a\u3051\u308c\u3070\u306a\u308a\u307e\u305b\u3093\u3002\u884c\u6570\u306f\u5897\u3048\u307e\u3059\u3057\u3001\u30e1\u30bd\u30c3\u30c9\u3092\u547c\u3073\u51fa\u3059\u56de\u6570\u304c\u591a\u3044\u306e\u3067\u6027\u80fd\u3082\u60aa\u305d\u3046\u3067\u3059\u3002\u305d\u3046\u898b\u308b\u3068\u3001\u6b63\u898f\u8868\u73fe\u306f\u52b9\u7387\u304c\u826f\u3044\u3067\u3059\u306d\u3002<\/p>\n<pre class=\"prettyprint lang:default decode:true\">String str = \"The quick brown fox jumps over the lazy dog\";\r\nString replaced = str.replace(\"a\", \"x\");\r\nreplaced = replaced.replace(\"b\", \"x\");\r\n:\r\nreplaced = replaced.replace(\"y\", \"x\");\r\nreplaced = replaced.replace(\"z\", \"x\"); \/\/ a\u304b\u3089z\u307e\u3067\u5ef6\u3005\u3068\u7d9a\u3051\u308b\u304b\u3001a-z\u3067\u30eb\u30fc\u30d7\u3059\u308b<\/pre>\n<p>\u3082\u3061\u308d\u3093\u3001\u6b63\u898f\u8868\u73fe\u3078\u306f\u65e5\u672c\u8a9e\u3082\u4f7f\u3048\u307e\u3059\u3002<span>(<\/span>\u3044\u308f\u3086\u308b<span>)<\/span>\u5168\u89d2\u534a\u89d2\u3082<span>Java<\/span>\u306e\u5185\u90e8\u3067\u306f\u304d\u3061\u3093\u3068\u5225\u7269\u306a\u306e\u3067\u6ce8\u610f\u3057\u307e\u3057\u3087\u3046\u3002<\/p>\n<pre class=\"prettyprint lang:default decode:true\">\/\/ \u65e5\u672c\u8a9e\u3067\u3082\u5927\u4e08\u592b!\r\nString str1 = \"\u79c1\u306f\u72ac\u304c\u5927\u597d\u304d\u3067\u3059\u3002\u72ac\u306f\u4eba\u985e\u306e\u53cb\u3067\u3059\u3002\";\r\nString str2 = \"\u9ce5\u985e\u3068\u306f\u3001\u9ce5\u7db1\u3059\u306a\u308f\u3061\u810a\u690e\u52d5\u7269\u4e9c\u9580\u306e\u4e00\u7db1\u306b\u5c5e\u3059\u308b\u52d5\u7269\u7fa4\u306e\u7dcf\u79f0\u3002\";\r\nString regex = \"[\u72ac\u9ce5]\"; \/\/ \u72ac\u3042\u308b\u3044\u306f\u732b\u306b\u30de\u30c3\u30c1\u3059\u308b\r\nSystem.out.println(str1.replaceAll(regex, \"\u732b\")); \/\/ \u2192 \u79c1\u306f\u732b\u304c\u5927\u597d\u304d\u3067\u3059\u3002\u732b\u306f\u4eba\u985e\u306e\u53cb\u3067\u3059\u3002\r\nSystem.out.println(str2.replaceAll(regex, \"\u732b\")); \/\/ \u2192 \u732b\u985e\u3068\u306f\u3001\u732b\u7db1\u3059\u306a\u308f\u3061\u810a\u690e\u52d5\u7269\u4e9c\u9580\u306e\u4e00\u7db1\u306b\u5c5e\u3059\u308b\u52d5\u7269\u7fa4\u306e\u7dcf\u79f0\u3002<\/pre>\n<h3><span id=\"-Java\">\uff11-\uff12\uff0eJava\u3067\u4f7f\u3048\u308b\u6b63\u898f\u8868\u73fe\u306e\u30d1\u30bf\u30fc\u30f3\u4f8b<\/span><\/h3>\n<p>\u6b63\u898f\u8868\u73fe\u3067\u306f\u4ee5\u4e0b\u306e\u3082\u306e\u3092\u899a\u3048\u3066\u304a\u3051\u3070\u6700\u521d\u306e\u5185\u306f\u5927\u4f53<span>OK<\/span>\u3067\u3059\u3002\u3082\u3063\u3068\u51dd\u3063\u305f\u3053\u3068\u3092\u3084\u308a\u305f\u3051\u308c\u3070\u3001\u305d\u3053\u304b\u3089\u3069\u3093\u3069\u3093\u5fdc\u7528\u3057\u3066\u3044\u3051\u3070\u3044\u3044\u306e\u3067\u3059\u3002<\/p>\n<blockquote>\n<p>[] \u2190 []\u306e\u4e2d\u306b\u6307\u5b9a\u3057\u305f\u6587\u5b57\u3068\u306e\u4e00\u81f4<\/p>\n<p>[a-z] \u2190 \u82f1\u8a9e\u5c0f\u6587\u5b57<\/p>\n<p>[A-Z] \u2190 \u82f1\u8a9e\u5927\u6587\u5b57<\/p>\n<p>[0-9] \u2190 \u6570\u5b57<\/p>\n<p>() \u2190 \u6587\u5b57\u5217\u306e\u30b0\u30eb\u30fc\u30d7\u5316<\/p>\n<p>| \u2190 \u30b0\u30eb\u30fc\u30d7\u5316\u3057\u305f\u6587\u5b57\u5217\u5185\u3067\u306e\u9078\u629e<\/p>\n<p>+ \u2190 \u6587\u5b57\u3042\u308b\u3044\u306f\u6587\u5b57\u5217\u304c<span>1<\/span>\u56de\u4ee5\u4e0a\u7e70\u308a\u8fd4\u3057\u51fa\u73fe\u3059\u308b<\/p>\n<p>* \u2190 \u6587\u5b57\u3042\u308b\u3044\u306f\u6587\u5b57\u5217\u304c<span>0<\/span>\u56de\u4ee5\u4e0a\u7e70\u308a\u8fd4\u3057\u51fa\u73fe\u3059\u308b<\/p>\n<p>^ \u2190 \u6587\u5b57\u5217\u306e\u5148\u982d<\/p>\n<p>$ \u2190 \u6587\u5b57\u5217\u306e\u672b\u5c3e<\/p>\n<\/blockquote>\n<p>\u306a\u304a\u3001<span>Java<\/span>\u3067\u4f7f\u3048\u308b\u6b63\u898f\u8868\u73fe\u306e\u30d1\u30bf\u30fc\u30f3\u306e\u8a73\u7d30\u306f\u3001\u4f8b\u3048\u3070<span>Javadoc<\/span>\u306e<span>java.util.regex.Pattern<\/span>\u3092\u3054\u89a7\u304f\u3060\u3055\u3044\u3002\u8a18\u8f09\u3055\u308c\u3066\u3044\u308b\u5206\u91cf\u306f\u591a\u3044\u3067\u3059\u304c\u3001\u5b9f\u52d9\u3067\u983b\u7e41\u306b\u4f7f\u3046\u306e\u306f\u305d\u306e\u4e2d\u306e\u4e00\u90e8\u3067\u3059\u3002<\/p>\n<h3><span id=\"-Patternquote\">\uff11-\uff13\uff0e\u7279\u6b8a\u6587\u5b57\u306e\u7121\u52b9\u5316\u306b\u306f<span>\\<\/span>\u304b<span>Pattern.quote<\/span>\u3092\u4f7f\u3046<\/span><\/h3>\n<p>\u6b63\u898f\u8868\u73fe\u3067\u306f\u3001\u524d\u8ff0\u306e\u3068\u304a\u308a<span>[]<\/span>\u3001<span>()<\/span>\u3001<span>\\<\/span>\u3001<span>|<\/span>\u306a\u3069\u3044\u304f\u3064\u304b\u306e\u6587\u5b57\u3092\u7279\u5225\u306a\u610f\u5473\u306b\u7528\u3044\u307e\u3059\u3002\u3053\u308c\u3089\u306e\u6587\u5b57\u3092\u305d\u306e\u307e\u307e\u6b63\u898f\u8868\u73fe\u4e2d\u3067\u6307\u5b9a\u3057\u305f\u3044\u5834\u5408\u306f\u3001<span>\\<\/span>\u306b\u3088\u308a\u7121\u52b9\u5316\u3057\u306a\u3051\u308c\u3070\u306a\u308a\u307e\u305b\u3093<span>(<\/span>\u4e00\u822c\u306b\u306f\u30a8\u30b9\u30b1\u30fc\u30d7\u3068\u547c\u3070\u308c\u307e\u3059<span>)<\/span>\u3002<\/p>\n<p>\u4ee5\u4e0b\u306e\u4f8b\u3067\u306f\u3001\u6b63\u898f\u8868\u73fe\u3067\u306f\u6587\u5b57\u3092\u9078\u629e\u6307\u5b9a\u3059\u308b<span> [] <\/span>\u81ea\u4f53\u3092\u7f6e\u63db\u3057\u305f\u3044\u306e\u3067\u3059\u304c\u3001\u305d\u306e\u307e\u307e\u3067\u306f\u6b63\u898f\u8868\u73fe\u306e\u6587\u6cd5\u30a8\u30e9\u30fc\u306b\u306a\u3063\u3066\u3057\u307e\u3044\u307e\u3059\u3002<\/p>\n<pre class=\"prettyprint lang:default decode:true\">String str = \"Java\u3067\u306f\u914d\u5217\u306e\u8981\u7d20\u306f[]\u306e\u4e2d\u306b\u6dfb\u3048\u5b57\u3092\u66f8\u3044\u3066\u30a2\u30af\u30bb\u30b9\u3057\u307e\u3059\u3002\";\r\nString replaced = str.replaceAll(\"[]\", \"\u89d2\u62ec\u5f27\"); \/\/ \u2192 java.util.regex.PatternSyntaxException: Unclosed character class near index 1\r\nSystem.out.println(replaced);<\/pre>\n<p>\u3053\u308c\u3092\u52d5\u304f\u3088\u3046\u306b\u3059\u308b\u306b\u306f\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u3057\u3066\u7279\u5225\u306a\u610f\u5473\u3092\u6301\u3064\u6587\u5b57\u3092\u30a8\u30b9\u30b1\u30fc\u30d7\u3057\u307e\u3059\u3002<span>\\\\<\/span>\u3092\u4e8c\u3064\u66f8\u304f\u306e\u306f\u3001\u6700\u7d42\u7684\u306b<span>\\[\\]<\/span>\u3068\u3044\u3046\u6587\u5b57\u5217\u306b\u3057\u305f\u3044\u304b\u3089\u3067\u3059\u3002<span>Java<\/span>\u306e<span> &#8220;&#8221; <\/span>\u306e\u4e2d\u3067\u306f\u3001<span>\\<\/span>\u81ea\u4f53\u304c\u7279\u5225\u306a\u610f\u5473\u3092\u6301\u3064\u304b\u3089\u3067\u3059\u306d\u3002<\/p>\n<pre class=\"prettyprint lang:default decode:true\">String str = \"Java\u3067\u306f\u914d\u5217\u306e\u8981\u7d20\u306f[]\u306e\u4e2d\u306b\u6dfb\u3048\u5b57\u3092\u66f8\u3044\u3066\u30a2\u30af\u30bb\u30b9\u3057\u307e\u3059\u3002\";\r\nString replaced = str.replaceAll(\"\\\\[\\\\]\", \"\u89d2\u62ec\u5f27\"); \/\/ \u2192 OK!!\r\nSystem.out.println(replaced); \/\/ \u2192 Java\u3067\u306f\u914d\u5217\u306e\u8981\u7d20\u306f\u89d2\u62ec\u5f27\u306e\u4e2d\u306b\u6dfb\u3048\u5b57\u3092\u66f8\u3044\u3066\u30a2\u30af\u30bb\u30b9\u3057\u307e\u3059\u3002<\/pre>\n<p>\u305f\u3060\u3057\u3001\u3044\u3061\u3044\u3061\u624b\u3067\u30a8\u30b9\u30b1\u30fc\u30d7\u3059\u308b\u306e\u3082\u5927\u5909\u306a\u306e\u3067\u3001\u307e\u3068\u3081\u3066\u5168\u90e8\u30a8\u30b9\u30b1\u30fc\u30d7\u3057\u305f\u3044\u306a\u3089<span>Pattern.quote<\/span>\u304c\u4f7f\u3048\u307e\u3059\u3002\u5b9f\u884c\u7d50\u679c\u306b\u4ed8\u304f<span>\\Q<\/span>\u3068<span>\\E<\/span>\u306f\u3001\u7bc4\u56f2\u5185\u306e\u5168\u3066\u306e\u7279\u6b8a\u6587\u5b57\u3092\u30a8\u30b9\u30b1\u30fc\u30d7\u3059\u308b\u3082\u306e\u3067\u3059\u3002\u6b63\u78ba\u306b\u884c\u3046\u306b\u306f\u8272\u3005\u8003\u616e\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u304c\u3001\u305d\u308c\u3092\u5168\u90e8\u3084\u3063\u3066\u304f\u308c\u307e\u3059\u3002<\/p>\n<pre class=\"prettyprint lang:default decode:true\">\/ Pattern\u3067\u306e\u30e1\u30bd\u30c3\u30c9\u5b9a\u7fa9\r\npublic static String quote(String s)\r\n\ts - \u30ea\u30c6\u30e9\u30eb\u5316\u3059\u308b\u6587\u5b57\u5217<\/pre>\n<pre class=\"prettyprint lang:default decode:true\">String str = \"Java\u3067\u306f\u914d\u5217\u306e\u8981\u7d20\u306f[]\u306e\u4e2d\u306b\u6dfb\u3048\u5b57\u3092\u66f8\u3044\u3066\u30a2\u30af\u30bb\u30b9\u3057\u307e\u3059\u3002\";\r\nString regex = Pattern.quote(\"[]\");\r\nString replaced = str.replaceAll(regex, \"\u89d2\u62ec\u5f27\"); \/\/ \u2192 OK!!\r\nSystem.out.println(replaced); \/\/ \u2192 Java\u3067\u306f\u914d\u5217\u306e\u8981\u7d20\u306f\u89d2\u62ec\u5f27\u306e\u4e2d\u306b\u6dfb\u3048\u5b57\u3092\u66f8\u3044\u3066\u30a2\u30af\u30bb\u30b9\u3057\u307e\u3059\u3002\r\nSystem.out.println(regex); \/\/ \u2192 \\Q[]\\E<\/pre>\n<h2><span id=\"JavaStringreplaceFirst\">\uff12\uff0e\u3010Java\u3011String.replaceFirst\u306f\u6700\u521d\u306e\u30de\u30c3\u30c1\u7b87\u6240\u3060\u3051\u3092\u7f6e\u63db\u3059\u308b<\/span><\/h2>\n<p>String.replaceFirst\u306f\u3001<span>replaceAll<\/span>\u306e\u9650\u5b9a\u7248\u3067\u3001\u6b63\u898f\u8868\u73fe\u306b\u30de\u30c3\u30c1\u3057\u305f\u6700\u521d\u306e\u4e00\u7b87\u6240\u76ee\u3060\u3051\u3092\u7f6e\u63db\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"prettyprint lang:default decode:true\">public String replaceFirst(String regex, String replacement)\r\n\tregex - \u3053\u306e\u6587\u5b57\u5217\u3068\u306e\u4e00\u81f4\u3092\u5224\u5b9a\u3059\u308b\u6b63\u898f\u8868\u73fe\r\n\treplacement - \u6700\u521d\u306b\u4e00\u81f4\u3059\u308b\u3082\u306e\u306b\u7f6e\u304d\u63db\u3048\u3089\u308c\u308b\u6587\u5b57\u5217<\/pre>\n<p>\u3067\u3059\u306e\u3067\u3001\u4ee5\u4e0b\u306e\u4f8b\u3067\u306f\u6700\u521d\u306b\u51fa\u73fe\u3057\u305f\u82f1\u8a9e\u5c0f\u6587\u5b57<span>(The<\/span>\u306e<span>h)<\/span>\u306e\u307f\u304c\u7f6e\u63db\u3055\u308c\u307e\u3057\u305f\u3002<\/p>\n<pre class=\"prettyprint lang:default decode:true\">String str = \"The quick brown fox jumps over the lazy dog. 1234567890\";\r\nString replaced = str.replaceFirst(\"[a-z]\", \"x\");\r\nSystem.out.println(replaced); \/\/ \u2192 Txe quick brown fox jumps over the lazy dog. 1234567890<\/pre>\n<h2><span id=\"StringreplaceAll\">\uff13\uff0e\u3010\u5fdc\u7528\u3011String.replaceAll\u306e\u5fdc\u7528\u4f8b<\/span><\/h2>\n<p>String.replaceAll\/replaceFirst\u306e\u5f37\u307f\u306f\u3001\u7f6e\u63db\u5bfe\u8c61\u306e\u6587\u5b57\u5217\u3092\u6b63\u898f\u8868\u73fe\u3067\u6307\u5b9a\u3067\u304d\u308b\u3053\u3068\u3067\u3059\u3002\u3053\u306e\u6307\u5b9a\u3092\u3069\u3046\u5de5\u592b\u3059\u308b\u304b\u3067\u3001\u8272\u3005\u306a\u5fdc\u7528\u304c\u3067\u304d\u308b\u306e\u3067\u3059\u3002\u4ee5\u4e0b\u306f\u305d\u306e\u4e00\u4f8b\u3067\u3059\u3002<\/p>\n<h3><span id=\"i-2\">\uff13-\uff11\uff0e\u7a7a\u767d\u306e\u524a\u9664\u3084\u8a70\u3081<\/span><\/h3>\n<p>\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u7a7a\u767d\u524a\u9664\u306b\u3082\u4f7f\u3048\u307e\u3059\u3002\u7c21\u5358\u304b\u3064\u5730\u5473\u3067\u3059\u304c\u3001\u5229\u7528\u983b\u5ea6\u306f\u7d50\u69cb\u9ad8\u3044\u3067\u3059\u3088\u3002\u7f6e\u63db\u5148\u306e\u6587\u5b57\u5217\u3092\u7a7a\u6587\u5b57\u5217<span>(<\/span>\u9577\u3055<span>0<\/span>\u306e\u6587\u5b57\u5217\u3001<span>&#8220;&#8221;)<\/span>\u3068\u3059\u308c\u3070\u3001\u524a\u9664\u3068\u540c\u3058\u610f\u5473\u306b\u306a\u308b\u306e\u3067\u3059\u3002<\/p>\n<pre class=\"prettyprint lang:default decode:true\">String str = \"A B C D\";\r\nString replaced = str.replaceAll(\" \", \"\");\r\nSystem.out.println(replaced); \/\/ \u2192 ABCD<\/pre>\n<p>\u7a7a\u767d\u6271\u3044\u3057\u305f\u3044\u6587\u5b57\u306f\u6b63\u898f\u8868\u73fe\u3067\u81ea\u7531\u306b\u6307\u5b9a\u3067\u304d\u307e\u3059\u306e\u3067\u3001\u4f8b\u3048\u3070\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u3082\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre class=\"prettyprint lang:default decode:true\">String str = \"A B\\tC\\nD\"; \/\/ \u30bf\u30d6(\\t)\u3068\u6539\u884c(\\n)\u3092\u542b\u3080\r\nString replaced = str.replaceAll(\"[ \\t\\n] \", \"\");\r\nSystem.out.println(replaced); \/\/ \u2192 ABCD<\/pre>\n<p>\u30d1\u30bf\u30fc\u30f3\u3092\u5c11\u3057\u5909\u3048\u308b\u3060\u3051\u3067\u3001\u8907\u6570\u306e\u7a7a\u767d\u3092<span>1<\/span>\u3064\u306b\u307e\u3068\u3081\u308b\u306e\u3082\u7c21\u5358\u306b\u3067\u304d\u307e\u3059\u3002\u3044\u304f\u3064\u7a7a\u767d\u304c\u3042\u3063\u3066\u3082\u30d1\u30bf\u30fc\u30f3\u306b\u30de\u30c3\u30c1\u3059\u308c\u3070\u3044\u3044\u3001\u3068\u3044\u3046\u3053\u3068\u304c\u5927\u304d\u306a\u5f37\u307f\u3067\u3059\u3002<\/p>\n<pre class=\"prettyprint lang:default decode:true\">String str = \"A      B    C D\";\r\nString replaced = str.replaceAll(\" +\", \" \");\r\nSystem.out.println(replaced); \/\/ \u2192 \"A B C D\"<\/pre>\n<h3><span id=\"-trim\">\uff13-\uff12\uff0e\u5168\u89d2\u30fb\u534a\u89d2\u7a7a\u767d\u4e21\u5bfe\u5fdc<span>trim<\/span><\/span><\/h3>\n<p>\u6a19\u6e96\u306e<span>String.trim<\/span>\u306f\u7a7a\u767d<span>(\u203b)<\/span>\u3060\u3051\u524a\u9664\u3057\u3066\u304f\u308c\u307e\u3059\u304c\u3001\u79c1\u305f\u3061\u65e5\u672c\u4eba\u3060\u3068<span>(<\/span>\u3044\u308f\u3086\u308b<span>)<\/span>\u5168\u89d2\u7a7a\u767d\u3082<span>trim<\/span>\u3057\u305f\u3044\u3067\u3059\u3088\u306d\u3002\u305d\u306e\u5834\u5408\u3082\u3001<span>replaceFirst<\/span>\u3092\u4f7f\u3048\u3070\u697d\u3005\u3067\u3059\u3002<span>\u203b<\/span>\u6b63\u78ba\u306b\u306f\u7a7a\u767d\u3088\u308a\u30b3\u30fc\u30c9\u30dd\u30a4\u30f3\u30c8\u304c\u5c0f\u3055\u3044\u6587\u5b57<\/p>\n<pre class=\"prettyprint lang:default decode:true\">String str = \" \u3000 A B C D \u3000 \"; \/\/ \u2190\u5148\u982d\u30fb\u672b\u5c3e\u306b\u5168\u89d2\u534a\u89d2\u7a7a\u767d\u304c\u6df7\u5728\u3057\u3066\u3044\u307e\u3059\r\nString replaced = str.replaceFirst(\"^[\\\\s\u3000]+\", \"\").replaceFirst(\"[\\\\s\u3000]+$\", \"\");\r\nSystem.out.println(replaced); \/\/ \u2192 \"A B C D\"<\/pre>\n<p>\u8981\u306f\u3001\u5148\u982d<span>(^)<\/span>\u306e\u5f8c\u3068\u672b\u5c3e<span>($)<\/span>\u306e\u524d\u3067\u7a7a\u767d\u6271\u3044\u3057\u305f\u3044\u3082\u306e\u304c\u3042\u308c\u3070\u3001\u305d\u308c\u3092<span>&#8220;&#8221;<\/span>\u306b\u7f6e\u63db\u3057\u3066\u3044\u308b\u3060\u3051\u3067\u3059\u3002<span>replaceAll<\/span>\u3067\u3082\u7d50\u679c\u306f\u540c\u3058\u3067\u3059\u304c\u3001<span>1<\/span>\u56de\u3060\u3051\u306b\u306a\u308b\u306e\u3067\u3053\u3061\u3089\u306e\u65b9\u304c\u610f\u5473\u304c\u660e\u78ba\u306b\u306a\u308b\u3067\u3057\u3087\u3046\u3002<\/p>\n<p>trim\u3059\u308b\u7bc4\u56f2\u3092\u5148\u982d\u3060\u3051\u3001\u672b\u5c3e\u3060\u3051\u3068\u3057\u305f\u3044\u306a\u3089\u3001\u3069\u3061\u3089\u304b\u7247\u65b9\u306e<span>replaceFirst<\/span>\u3060\u3051\u3092\u5b9f\u884c\u3059\u308c\u3070<span>OK<\/span>\u3067\u3059\u3002<\/p>\n<h3><span id=\"i-3\">\uff13-\uff13\uff0e\u7279\u5b9a\u6587\u8a00\u306e\u30de\u30b9\u30af\u30fb\u6587\u8a00\u306e\u4fee\u6b63<\/span><\/h3>\n<p>\u4f55\u304b\u306e\u6587\u7ae0\u306e\u4e2d\u306b\u542b\u307e\u308c\u308b\u7279\u5b9a\u30ad\u30fc\u30ef\u30fc\u30c9\u3092\u30de\u30b9\u30af\u3057\u305f\u3044\u5834\u5408\u304c\u3042\u308a\u307e\u3059\u3002<span>replace<\/span>\u3067\u3082\u826f\u3044\u3067\u3059\u304c\u3001\u6570\u304c\u591a\u3044\u5834\u5408\u3060\u3068\u4e00\u3064\u306e\u30d1\u30bf\u30fc\u30f3\u3067\u5168\u90e8\u8cc4\u3063\u3066\u3057\u307e\u3063\u305f\u65b9\u304c\u697d\u306a\u5834\u5408\u3082\u3042\u308a\u307e\u3059\u3002\u30ad\u30fc\u30ef\u30fc\u30c9\u3092<span>|<\/span>\u3067\u7e4b\u3044\u3067\u3044\u304f\u3060\u3051\u3067\u3059\u3002<\/p>\n<pre class=\"prettyprint lang:default decode:true\">String str = \"The quick brown fox jumps over the lazy dog\";\r\nString replaced = str.replaceAll(\"fox|dog\", \"xxx\");\r\nSystem.out.println(replaced); \/\/ \u2192 The quick brown xxx jumps over the lazy xxx<\/pre>\n<p>\u3061\u306a\u307f\u306b\u3001\u5927\u6587\u5b57\u30fb\u5c0f\u6587\u5b57\u3092\u7121\u8996\u3057\u305f\u3044\u306a\u3089\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u3082\u3067\u304d\u307e\u3059\u3002<span>(?i)<\/span>\u306f\u5927\u6587\u5b57\u30fb\u5c0f\u6587\u5b57\u306e\u533a\u5225\u3092\u306a\u304f\u3059\u305f\u3081\u306e\u30aa\u30d7\u30b7\u30e7\u30f3\u3067\u3059\u3002\u3053\u306e\u4ed6\u306b\u3082\u5168\u89d2\u534a\u89d2\u3092\u540c\u4e00\u8996\u3059\u308b\u3082\u306e\u306a\u3069\u8272\u3005\u306a\u30aa\u30d7\u30b7\u30e7\u30f3\u304c\u3042\u308a\u307e\u3059\u306e\u3067\u3001\u8208\u5473\u304c\u3042\u308c\u3070\u300cjava \u6b63\u898f\u8868\u73fe \u30aa\u30d7\u30b7\u30e7\u30f3\u300d\u306a\u3069\u306e\u30ad\u30fc\u30ef\u30fc\u30c9\u3067\u8abf\u3079\u3066\u307f\u3066\u3082\u3044\u3044\u3067\u3057\u3087\u3046\u3002<\/p>\n<pre class=\"prettyprint lang:default decode:true\">String str = \"The quick brown FoX jumps over the lazy dOg\";\r\nString replaced = str.replaceAll(\"(?i)fox|dog\", \"xxx\");\r\nSystem.out.println(replaced); \/\/ \u2192 The quick brown xxx jumps over the lazy xxx<\/pre>\n<p>\u3053\u308c\u306f\u3061\u3087\u3063\u3068\u3057\u305f\u6587\u8a00\u306e\u4fee\u6b63\u306b\u3082\u4f7f\u3048\u307e\u3059\u3002\u3053\u306e\u4f8b\u306f\u97f3\u5f15\u304d\u306e\u4fee\u6b63\u3067\u3001\u6587\u4e2d\u306e\u300c\u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u300d\u3042\u308b\u3044\u306f\u300c\u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u30fc\u300d\u3092\u3069\u3061\u3089\u304b\u306b\u7d71\u4e00\u3059\u308b\u3082\u306e\u3067\u3059\u3002\u3053\u3053\u304b\u3089\u5de5\u592b\u3092\u3059\u308c\u3070\u3001\u9001\u308a\u4eee\u540d\u306e\u7d71\u4e00\u306b\u3082\u4f7f\u3048\u307e\u3059\u306d\u3002<\/p>\n<pre class=\"prettyprint lang:default decode:true\">String str = \"\u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u30fc\u3068\u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u306f\u540c\u3058\u3082\u306e?\";\r\nString replaced1 = str.replaceAll(\"(\u30b3\u30f3\u30d4\u30e5\u30fc\u30bf)([\u30fc])\", \"$1\");\r\nSystem.out.println(replaced1); \/\/ \u2192 \u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u3068\u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u306f\u540c\u3058\u3082\u306e?\r\nString replaced2 = str.replaceAll(\"(\u30b3\u30f3\u30d4\u30e5\u30fc\u30bf)([^\u30fc])\", \"$1\u30fc$2\");\r\nSystem.out.println(replaced2); \/\/ \u2192 \u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u30fc\u3068\u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u30fc\u306f\u540c\u3058\u3082\u306e?<\/pre>\n<h3><span id=\"i-4\">\uff13-\uff14\uff0e\u6539\u884c\u30b3\u30fc\u30c9\u3084\u533a\u5207\u308a\u6587\u5b57\u306e\u7d71\u4e00<\/span><\/h3>\n<p>1\u3064\u306e\u6587\u5b57\u5217\u3084\u30d5\u30a1\u30a4\u30eb\u306e\u4e2d\u306b\u6539\u884c\u30b3\u30fc\u30c9\u304c\u6df7\u5728\u3057\u3066\u3044\u308b\u3053\u3068\u304c\u3042\u308a\u307e\u3059\u3002<span>Windows<\/span>\u7cfb\u306a\u3089<span>\\r\\n<\/span>\u3001<span>UNIX<\/span>\u7cfb\u306a\u3089<span>\\n<\/span>\u3001\u53e4\u3044<span>Mac<\/span>\u306a\u3089<span>\\r<\/span>\u3067\u3059\u3002\u305d\u308c\u3092\u7d71\u4e00\u3059\u308b\u306e\u3082\u7c21\u5358\u3067\u3059\u3002\u6b63\u898f\u8868\u73fe\u306e\u4e2d\u3067\u306f\u3001\\\u3092\u6307\u5b9a\u3057\u305f\u3044\u5834\u5408\u306f\\\\\u3092\u4e8c\u3064\u91cd\u306d\u308b\u5fc5\u8981\u304c\u3042\u308b\u3053\u3068\u306b\u6ce8\u610f\u3057\u307e\u3057\u3087\u3046\u3002<\/p>\n<pre class=\"prettyprint lang:default decode:true\">String str = \"A\\r\\nB\\rC\\nD\";\r\nString replaced = str.replaceAll(\"\\\\r\\\\n|\\\\r|\\\\n\", \"\\r\\n\");\r\nSystem.out.println(replaced); \/\/ \u2192 \"A\\r\\nB\\r\\nC\\r\\nD\"\r\nSystem.out.println(Arrays.toString(replaced.getBytes())); \/\/ \u2192 [65, 13, 10, 66, 13, 10, 67, 13, 10, 68]<\/pre>\n<p>\u8907\u6570\u3042\u308b\u533a\u5207\u308a\u6587\u5b57\u3092\u4e00\u3064\u306b\u3059\u308b\u306e\u3082\u540c\u3058\u3088\u3046\u306b\u4e00\u767a\u3067\u3059\u3002\u30ed\u30b0\u306a\u3069\u3092\u5206\u6790\u3057\u3084\u3059\u304f\u3059\u308b\u305f\u3081\u3001\u30bf\u30d6\u306a\u3069\u306b\u3072\u3068\u307e\u3068\u3081\u306b\u3059\u308b\u306e\u306b\u4f7f\u3048\u307e\u3059\u306d\u3002<\/p>\n<pre class=\"prettyprint lang:default decode:true\">String str = \"A,B\\tC-D\";\r\nString replaced = str.replaceAll(\"[,\\t\\\\-]\", \",\");\r\nSystem.out.println(replaced); \/\/ \u2192 A,B,C,D<\/pre>\n<h3><span id=\"-HTMLXML\">\uff13-\uff15\uff0eHTML\/XML\u4e2d\u306e\u5c5e\u6027\u5024\u306e\u524a\u9664<\/span><\/h3>\n<p>HTML\u4e2d\u3067\u7279\u5b9a\u306e\u6761\u4ef6\u3092\u6e80\u305f\u3059\u5c5e\u6027\u5024<span>(attribute)<\/span>\u3092\u3054\u3063\u305d\u308a\u524a\u9664\u3067\u304d\u305f\u308a\u3057\u307e\u3059\u3002\u524a\u9664\u3057\u305f\u3044\u6761\u4ef6\u304c\u3001\u6b63\u898f\u8868\u73fe\u3067\u4e0a\u624b\u304f\u8868\u73fe\u3067\u304d\u308b\u304b\u304c\u30dd\u30a4\u30f3\u30c8\u3067\u3059\u3002\u3053\u3053\u3067\u306f\u3001\u5c5e\u6027\u540d\u304c<span>height<\/span>\u304b\u3064\u5c5e\u6027\u5024\u304c\u6570\u5024\u306e\u3082\u306e\u3092\u9078\u3093\u3067\u3044\u307e\u3059\u3002<\/p>\n<pre class=\"prettyprint lang:default decode:true\">String str = \"&lt;img height=\\\"100\\\" class=\\\"main\\\" \/&gt;&lt;img height=\\\"50\\\" class=\\\"footer\\\" \/&gt;\";\r\nString replaced = str.replaceAll(\" height=\\\"\\\\d+\\\"\", \"\");\r\nSystem.out.println(replaced); \/\/ \u2192 &lt;img class=\"main\" \/&gt;&lt;img class=\"footer\" \/&gt;<\/pre>\n<h2><span id=\"StringreplaceAll-2\">\uff14\uff0e\u3010\u4e2d\u7d1a\u8005\u5411\u3051\u3011<span>String.replaceAll<\/span>\u306e\u3082\u3046\u5c11\u3057\u9032\u3093\u3060\u8a71\u984c<\/span><\/h2>\n<h3><span id=\"-012\">\uff14-\uff11\uff0e\u30de\u30c3\u30c1\u3057\u305f\u6587\u5b57\u5217\u306e\u53c2\u7167<span>($0<\/span>\u3001<span>$1<\/span>\u3001<span>$2\u2026)<\/span><\/span><\/h3>\n<p>String.replaceAll\u306e<span>2<\/span>\u756a\u76ee\u306e\u5f15\u6570\u306e\u4e2d\u3067\u306f\u3001\u6b63\u898f\u8868\u73fe\u4e2d\u3067\u300c<span>()<\/span>\u300d\u3067\u56f2\u3093\u3060\u90e8\u5206\u3092\u300c<span>$ + <\/span>\u6570\u5024\u300d\u3068\u3044\u3046\u5f62\u5f0f\u3067\u53c2\u7167\u3067\u304d\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"prettyprint lang:default decode:true\">String str = \"1000,5000,10000\";\r\nString replaced = str.replaceAll(\"([0-9]+)\", \"$0\u5186\");\r\nSystem.out.println(replaced); \/\/ \u2192 1000\u5186,5000\u5186,10000\u5186<\/pre>\n<p>\u3053\u3053\u3067\u3001<span>$0<\/span>\u306f\u30de\u30c3\u30c1\u3057\u305f\u90e8\u5206\u5168\u4f53\u3067\u3001\u4e0a\u8a18\u306e\u4f8b\u3067\u306f\u305d\u308c\u305e\u308c\u306e\u6570\u5b57\u90e8\u5206\u3067\u3059\u3002\u3053\u308c\u306f\u9806\u756a\u306b<span>$1<\/span>\u3001<span>$2<\/span>\u3001<span>$3\u2026<\/span>\u3068\u7d9a\u3051\u3089\u308c\u3001\u6b63\u898f\u8868\u73fe\u5185\u3067<span>()<\/span>\u306b\u3088\u308a\u56f2\u307e\u308c\u305f\u90e8\u5206\u3092\u9806\u756a\u306b\u53c2\u7167\u3067\u304d\u307e\u3059\u3002\u4ee5\u4e0b\u3067\u306f\u3001\u30de\u30c3\u30c1\u3057\u305f\u90e8\u5206\u306e\u4e26\u3073\u66ff\u3048\u3092\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<pre class=\"prettyprint lang:default decode:true\">String str = \"ABC1,D2EF,3GHI\";\r\nString replaced = str.replaceAll(\"([A-Z0-9]+),([A-Z0-9]+),([A-Z0-9]+)\", \"$2,$3,$1\");\r\nSystem.out.println(replaced); \/\/ \u2192 D2EF,3GHI,ABC1<\/pre>\n<p>\u3053\u308c\u306e\u4f7f\u3044\u3069\u3053\u308d\u306f\u5c11\u3057\u96e3\u3057\u3044\u3067\u3059\u3002\u3067\u3059\u304c\u3001\u5148\u8ff0\u3057\u305f\u5fdc\u7528\u4f8b\u3067\u3082\u4e00\u90e8\u3053\u306e\u6a5f\u80fd\u3092\u4f7f\u3063\u3066\u3044\u308b\u3068\u3053\u308d\u304c\u3042\u308a\u307e\u3059\u306e\u3067\u3001\u4f7f\u3044\u3088\u3046\u306b\u3088\u3063\u3066\u306f\u4fbf\u5229\u306a\u3082\u306e\u306a\u306e\u3067\u3059\u3002<\/p>\n<h3><span id=\"-PatternMatcher\">\uff14-\uff12\uff0ePattern\u3068Matcher\u3067\u6b63\u898f\u8868\u73fe\u306e\u4e16\u754c\u306b\u98db\u3073\u8fbc\u3082\u3046!<\/span><\/h3>\n<p>String.replaceAll\u306f\u3001\u5b9f\u306f\u6b63\u898f\u8868\u73fe\u306eAPI\u3067\u3042\u308bjava.util.regex.Pattern\u3068java.util.regex.Matcher\u3092\u610f\u8b58\u305b\u305a\u306b\u4f7f\u3046\u305f\u3081\u306e\u3082\u306e\u3067\u3059\u3002\u88cf\u3067\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u51e6\u7406\u304c\u884c\u308f\u308c\u3066\u3044\u307e\u3059\u3002<\/p>\n<pre class=\"prettyprint lang:default decode:true\">String str = \"The quick brown fox jumps over the lazy dog. 1234567890\";\r\nPattern pattern = Pattern.compile(\"[a-z]\");\r\nMatcher matcher = pattern.matcher(str);\r\nString replaced = matcher.replaceAll(\"x\");\r\nSystem.out.println(replaced); \/\/ \u2192 Txx xxxxx xxxxx xxx xxxxx xxxx xxx xxxx xxx. 1234567890<\/pre>\n<p>\u6b63\u898f\u8868\u73fe\u3067\u51dd\u3063\u305f\u3053\u3068\u3092\u3084\u308a\u305f\u3044\u5834\u5408\u306f\u3001<span>String.replaceAll<\/span>\u3060\u3051\u3067\u306f\u9650\u754c\u304c\u3042\u308a\u307e\u3059\u306e\u3067\u3001<span>Matcher<\/span>\u306e\u529b\u3092\u501f\u308a\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<p>\u4e00\u4f8b\u3068\u3057\u3066\u3001\u30de\u30c3\u30c1\u3057\u305f\u90e8\u5206\u3092\u3059\u3079\u3066\u51fa\u529b\u3059\u308b\u306b\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u3057\u307e\u3059\u3002<span>String.replaceAll<\/span>\u3067<span>$0<\/span>\u3084<span>$1<\/span>\u3001<span>$2<\/span>\u3067\u53c2\u7167\u3067\u304d\u3066\u3044\u305f\u3082\u306e\u3067\u3059\u3002<\/p>\n<pre class=\"prettyprint lang:default decode:true\">String str = \"1000,5000,10000\";\r\nPattern pattern = Pattern.compile(\"([0-9]+)\");\r\nMatcher matcher = pattern.matcher(str);\r\n\r\nwhile (matcher.find()) {\r\n\tSystem.out.println(matcher.group(0)); \/\/ \u2192 1000\u30015000\u300110000\u304c\u9806\u306b\u8868\u793a\u3055\u308c\u308b\r\n}<\/pre>\n<pre class=\"prettyprint lang:default decode:true\">String str = \"ABC1,D2EF,3GHI\";\r\nPattern pattern = Pattern.compile(\"([A-Z0-9]+),([A-Z0-9]+),([A-Z0-9]+)\");\r\nMatcher matcher = pattern.matcher(str);\r\n\r\nif (matcher.find()) {\r\n\tfor (int i = 1; i &lt;= matcher.groupCount(); i++) {\r\n\t\tSystem.out.println(matcher.group(i)); \/\/ \u2192 ABC1\u3001D2EF\u30013GHI\u304c\u9806\u306b\u8868\u793a\u3055\u308c\u308b\r\n\t}\r\n}<\/pre>\n<p>\u3053\u308c\u306f<span>Matcher<\/span>\u306e\u307b\u3093\u306e\u4e00\u6a5f\u80fd\u3067\u3059\u3002\u3082\u3063\u3068\u3044\u308d\u3044\u308d\u306a\u51c4\u3044\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u306e\u3067\u3001\u305c\u3072\u3084\u3063\u3066\u307f\u307e\u3057\u3087\u3046\u3002\u4f5c\u696d\u52b9\u7387\u5316\u306b\u7e4b\u304c\u308b\u3082\u306e\u304c\u3042\u308b\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u3002<\/p>\n<h2><span id=\"i-5\">\uff15\uff0e\u307e\u3068\u3081<\/span><\/h2>\n<p>String.replaceAll\u306f\u3001\u6b63\u898f\u8868\u73fe\u306e\u30d1\u30bf\u30fc\u30f3\u306b\u30de\u30c3\u30c1\u3059\u308b\u90e8\u5206\u3092\u6307\u5b9a\u3057\u305f\u6587\u5b57\u5217\u3067\u7f6e\u63db\u3059\u308b\u3082\u306e\u3067\u3059\u3002<span>replaceFirst<\/span>\u306f\u3001\u6700\u521d\u306b\u30de\u30c3\u30c1\u3057\u305f\u90e8\u5206\u3060\u3051\u7f6e\u63db\u3059\u308b\u3082\u306e\u3067\u3059\u3002<\/p>\n<p>\u3053\u306e\u8a18\u4e8b\u3067\u306f\u5fdc\u7528\u4f8b\u3092\u3044\u304f\u3064\u304b\u304a\u4f1d\u3048\u3057\u307e\u3057\u305f\u304c\u3001\u305d\u308c\u3067\u3082\u6b63\u898f\u8868\u73fe\u304c\u6301\u3064\u529b\u3092\u307b\u3093\u306e\u5c11\u3057\u57a3\u9593\u898b\u305f\u3060\u3051\u3067\u3059\u3002<span>String.replaceAll<\/span>\u306f\u3001\u6b63\u898f\u8868\u73fe\u3092\u7c21\u6613\u7684\u306b\u6271\u3046\u305f\u3081\u306e\u3082\u306e\u3067\u3057\u304b\u3042\u308a\u307e\u305b\u3093\u3002<span>Java<\/span>\u3067\u6b63\u898f\u8868\u73fe\u306b\u771f\u306e\u529b\u3092\u767a\u63ee\u3055\u305b\u308b\u305f\u3081\u306b\u306f\u3001<span>java.util.Pattern<\/span>\u3068<span>java.util.Matcher<\/span>\u3092\u6df1\u304f\u77e5\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<p>String.replaceAll\u3092\u8db3\u639b\u304b\u308a\u306b\u3001\u305c\u3072\u6b63\u898f\u8868\u73fe\u306e\u4e16\u754c\u306b\u8db3\u3092\u8e0f\u307f\u5165\u308c\u307e\u3057\u3087\u3046\u3002<span>Java<\/span>\u306b\u9650\u3089\u305a\u3001\u30a8\u30c7\u30a3\u30bf\u306a\u3069\u3067\u3082\u6b63\u898f\u8868\u73fe\u3092\u4f7f\u3048\u308b\u3053\u3068\u304c\u307b\u3068\u3093\u3069\u3067\u3059\u306e\u3067\u3001\u4e0a\u624b\u304f\u4f7f\u3044\u3053\u306a\u305b\u308c\u3070\u3042\u306a\u305f\u306e\u4f5c\u696d\u52b9\u7387\u304c\u4e00\u6c17\u306b\u826f\u304f\u306a\u308a\u307e\u3059\u3088<span>!!<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>String.replaceAll\u306f\u3001\u6587\u5b57\u5217\u4e2d\u3092\u6b63\u898f\u8868\u73fe\u3067\u30de\u30c3\u30c1\u30f3\u30b0\u3057\u3001\u30de\u30c3\u30c1\u3057\u305f\u90e8\u5206\u3092\u7f6e\u63db\u3059\u308b\u3082\u306e\u3067\u3059\u3002 \u540c\u3058\u3088\u3046\u306a\u52d5\u304d\u3092\u3059\u308b\u3082\u306e\u3068\u3057\u3066\u3001replaceFirst\u304c\u3042\u308a\u307e\u3059\u3002 \u3053\u306e\u8a18\u4e8b\u3067\u306f\u3001replaceAll\/rep [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":804,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[14],"acf":[],"_links":{"self":[{"href":"https:\/\/www.bold.ne.jp\/engineer-club\/wp-json\/wp\/v2\/posts\/985"}],"collection":[{"href":"https:\/\/www.bold.ne.jp\/engineer-club\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bold.ne.jp\/engineer-club\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bold.ne.jp\/engineer-club\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bold.ne.jp\/engineer-club\/wp-json\/wp\/v2\/comments?post=985"}],"version-history":[{"count":7,"href":"https:\/\/www.bold.ne.jp\/engineer-club\/wp-json\/wp\/v2\/posts\/985\/revisions"}],"predecessor-version":[{"id":5703,"href":"https:\/\/www.bold.ne.jp\/engineer-club\/wp-json\/wp\/v2\/posts\/985\/revisions\/5703"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.bold.ne.jp\/engineer-club\/wp-json\/wp\/v2\/media\/804"}],"wp:attachment":[{"href":"https:\/\/www.bold.ne.jp\/engineer-club\/wp-json\/wp\/v2\/media?parent=985"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bold.ne.jp\/engineer-club\/wp-json\/wp\/v2\/categories?post=985"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}