Matcher matches= 源字符串.matcher(正则);
while (matches.find()) {
String found= matches.group();
System.out.println(found);
}
正则有贪婪和非贪婪模式,所以你的最后集合只会有这两种的数据,不会出现如:zobo,boco。
代码片段:
Pattern pattern = Pattern.compile(".*?o");
Matcher matcher = pattern.matcher("zoboco");
while(matcher.find()){
String e=matcher.group(0);
System.out.println(e);
}
运行结果:
zo
bo
co