import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* group()返回由以前匹配操作所匹配的输入子序列
* group(int group)返回在以前匹配操作期间由给定组捕获的输入子序列
*
*/
public class RegexDemo {
public static void main(String[] args) {
String string = "12aa34 34bb45 55c66";
Pattern pattern = Pattern.compile("(\\d+)([a-z]+)(\\d+)");
Matcher matcher = pattern.matcher(string);
while(matcher.find()) {
System.out.println(matcher.group(0) + " "//group()默认返回group(0)
+ matcher.group(1) + " "
+ matcher.group(2) + " "
+ matcher.group(3));
}
String string2 = "123asfk0";
Pattern pattern2 = Pattern.compile("\\d+");
Matcher matcher2 = pattern2.matcher(string2);
System.out.println(matcher2.matches());//尝试将整个区域与模式匹配
}
}
输出:
12aa34 12 aa 34
34bb45 34 bb 45
55c66 55 c 66
false