<span style="font-size:14px;">package cn.itcast.test;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

//Greedy Reluctant Possessive区别
public class Demo3 {

public static void main(String[] args) {
/* //Greedy (.{3,10})[0-9] -->一般使用这个
//先把s吞取10个字符再说,与模式串进行匹配,匹配失败,把最后一个吐出来,吐出来的正好是数字,正好匹配,所以输出0-10
Pattern p = Pattern.compile("(.{3,10})[0-9]");
String s = "aaaa3bbbb6";
Matcher m = p.matcher(s);
if(m.find()){
p(m.start()+ "-" + m.end());
}else{
p("not match");
}*/

/*//Reluctant (.{3,10}?)[0-9]
//这个与Greedy正好相反 吞最少的 正好结果为0-5
Pattern p = Pattern.compile("(.{3,10}?)[0-9]");
String s = "aaaa3bbbb6";
Matcher m = p.matcher(s);
if(m.find()){
p(m.start()+ "-" + m.end());
}else{
p("not match");
}*/

// Possessive 与Greedy 不同的是,它不吐字符,这个匹配不上
Pattern p = Pattern.compile("(.{3,10}+)[0-9]");
String s = "aaaa3bbbb6";
Matcher m = p.matcher(s);
if(m.find()){
p(m.start()+ "-" + m.end());
}else{
p("not match");
}



}

private static void p(Object o) {
System.out.println(o);
}
}
</span>