public class Test{
public static void main(String[] args) throws Exception{
String regEx = "count(\\d+)(df)";
String s = "count000dfdfsdffaaaa1";
Pattern pat = Pattern.compile(regEx);
Matcher mat = pat.matcher(s);
if(mat.find()){
System.out.println(mat.group(2));
}
}
}
输出结果:
如果是mat.group()与 mat.group(0) 输出为 count000df,匹配整体字符串
如果是mat.group(1) 输出为 000,匹配第一个
如果是mat.group(2) 输出为 df,匹配第二个
public class ZhengZe {
public static void main(String[] args) throws Exception{
String regEx = "(count)(\\d+)(df)";
String s = "count000dfdfsdffaaaa1";
Pattern pat = Pattern.compile(regEx);
Matcher mat = pat.matcher(s);
if(mat.find()){
System.out.println(mat.group(0));
}
}
}
与例一不同的是给count也加上了括号,此时的输出结果:
如果是mat.group()与 mat.group(0) 输出为 count000df,匹配整体字符串
如果是mat.group(1) 输出为 count,匹配第一个
如果是mat.group(2) 输出为 000,匹配第二个
如果是mat.group(3) 输出为 df,匹配第三个
这也就是()的作用