最近使用正则replace时候遇到替换不正确的问题,如想替换an,实际上把can也替换成了c,下面的例子演示了如何删除完全匹配项。
import java.util.regex.Matcher; import java.util.regex.Pattern; public class 正则完全匹配_S4_Test { public static void main(String[] args) throws Exception { String str = "an aN can ban and sand AN aN AnD an pan CaN Can an An AN"; System.out.println("---------------拼接字符串-----------------"); String resultStr=replaceByAppend("(?<=\\s+|^)(an)(?=\\s+|$)",str); System.out.println(String.format("结果为%s,长度%s",resultStr,resultStr.length())); } public static String replaceByAppend(String regEx,String str) { Pattern pattern = Pattern.compile(regEx,Pattern.CASE_INSENSITIVE); Matcher matcher = pattern.matcher(str); StringBuffer sb = new StringBuffer(); int lastEnd = 0;//上一次匹配结束位置 while (matcher.find()) { sb.append(str.substring(lastEnd,matcher.start())); lastEnd = matcher.end() + 1; } if(lastEnd<str.length()){ sb.append(str.substring(lastEnd)); }else{ sb.deleteCharAt(sb.length()-1);//删除最后的空格 } return sb.toString(); } }
结果为:
---------------拼接字符串----------------- 结果为can ban and sand AnD pan CaN Can,长度32
---------------------------------------------------2014-10-23修改----------------------------------------------------------
import java.util.regex.Matcher; import java.util.regex.Pattern; public class 正则完全匹配_S4_Test { public static void main(String[] args) throws Exception { String str = "an aN AN An can banan an an ban and sand AN Ban anD aN AnD an pan CaN Can an An AN an's an"; System.out.println("---------------拼接字符串-----------------"); String resultStr = replaceByAppend("(?<=\\s+|^)(an)(?=\\s+|$)",resultStr.length())); System.out.println("---------------正则-----------------"); String resultStr2 = replaceRegAll("(?<=\\s+|^)(an)(?=\\s+|$)",resultStr2,resultStr2.length())); String resultStr3=str.replaceAll("(?i)(?<=\\s+|^)(an)(?=\\s+|$)",""); System.out.println(String.format("结果为%s,resultStr3,resultStr3.length())); String resultStr4 =replaceRegAll2("(\\s+)(an)(\\s+)",str); resultStr4 =replaceRegAll2("(^an)(\\s+)",resultStr4); resultStr4 =replaceRegAll2("(\\s+)(an$)",resultStr4); System.out.println(String.format("结果为%s,resultStr4,resultStr4.length())); String str2="$name2 $name $name name"; System.out.println(replaceRegFirst(str2,"$name","$****")); } public static String replaceByAppend(String regEx,Pattern.CASE_INSENSITIVE); Matcher matcher = pattern.matcher(str); StringBuffer sb = new StringBuffer(); int lastEnd = 0;// 上一次匹配结束位置 while (matcher.find()) { sb.append(str.substring(lastEnd,matcher.start())); lastEnd = matcher.end() + 1; } if (lastEnd < str.length()) { sb.append(str.substring(lastEnd)); } else { sb.deleteCharAt(sb.length() - 1);// 删除最后的空格 } return sb.toString(); } public static String replaceRegAll(String regEx,Pattern.CASE_INSENSITIVE); Matcher matcher = pattern.matcher(str); str = matcher.replaceAll(""); return str; } public static String replaceRegAll2(String regEx,Pattern.CASE_INSENSITIVE); Matcher matcher = pattern.matcher(str); while (matcher.find()) { str = matcher.replaceAll(" "); matcher = pattern.matcher(str); } return str; } // 替换First reg视为一般字符串匹配 public static String replaceRegFirst(String str,String reg,String replace) { replace = replace == null ? "" : replace; return Pattern.compile(Pattern.quote(reg)).matcher(str).replaceFirst(Matcher.quoteReplacement(replace)); } }
结果为:
---------------正则----------------- 结果为 can banan ban and sand Ban anD AnD pan CaN Can an's,长度65 结果为 can banan ban and sand Ban anD AnD pan CaN Can an's,长度65 结果为 can banan ban and sand Ban anD AnD pan CaN Can an's,长度53 $****2 $name $name name
全文完。