java replaceAll 的类似group用法

replaceAll比较特殊的用法介绍一个:

eg:把中括号及期内容替换成原先的再加Extra

"abc[def]".replaceAll("(\\[.*\\])", "$1Extra")

 结果:abc[def]Extra

 

 其中$1相当于matcher.group(1)的作用。附一个matcher的例子:

 

Pattern p3 = Pattern.compile("a(b*)(c*)d");
String candidateString3 = "abbccd abbbcccd";
Matcher matcher3 = p3.matcher(candidateString3);
System.out.println(matcher3.groupCount());
while (matcher3.find()) {
	System.out.println(matcher3.start());
	System.out.println("Group 0 " + matcher3.group(0));
	System.out.println("Group 1 " + matcher3.group(1));
	System.out.println("Group 2 " + matcher3.group(2));
}

 

2
0
Group 0 abbccd
Group 1 bb
Group 2 cc
7
Group 0 abbbcccd
Group 1 bbb
Group 2 ccc 

 

 

java字符串替换:大小写敏感和不敏感,一句代码解决  

(转自http://blog.csdn.net/zhen520/article/details/5590415)

public static void replaceString(String a,String b,String c){
          System.out.println(a);
          String result = a.replaceAll("(?i)"+b, c); //大小写不敏感
 
          System.out.println(result);
          System.out.println(a.replaceAll(b, c));//大小写敏感
 
}
 


 

 

你可能感兴趣的:(replaceAll)