JAVA正则表达式怎么匹配所有符合要求的子字符串

2025-02-28 21:02:03
推荐回答(2个)
回答(1):

Matcher matches= 源字符串.matcher(正则);

while (matches.find()) {
String found= matches.group();
System.out.println(found);
}

回答(2):

正则有贪婪和非贪婪模式,所以你的最后集合只会有这两种的数据,不会出现如: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