// *、+ 为贪婪模式,最大限度的匹配
public class Test001 {
public static void main(String[] args) throws Exception {
String text = "John writes about this, and John Doe writes about that, and John Wayne writes about everything.";
// *、+ 为贪婪模式,最大限度的匹配
Pattern pattern = Pattern.compile("(John)(.+) ");
Matcher matcher = pattern.matcher(text);
while(matcher.find()){
System.out.println(matcher.group(1)+matcher.group(2));
}
}
}
在 *、+ 或 ? 限定符之后放置 ?,该表达式从"贪心"表达式转换为"非贪心"表达式或者最小匹配。
public class Test001 {
public static void main(String[] args) throws Exception {
String text = "John writes about this, and John Doe writes about that, and John Wayne writes about everything.";
//在 *、+ 或 ? 限定符之后放置 ?,该表达式从"贪心"表达式转换为"非贪心"表达式或者最小匹配。
Pattern pattern = Pattern.compile("(John)(.+?) ");
Matcher matcher = pattern.matcher(text);
while(matcher.find()){
System.out.println(matcher.group(1)+matcher.group(2));
}
}
}