java正则表达式中:
java正则表达式中:
已知:
//定义f1("a", "aaa");作用,返回str中找到的符合 正则表达式reg的子串
public static void f1(String reg, String str) {//作用,返回str中找到的符合 正则表达式reg的子串
System.out.print(reg + "\t");
String reg2 = reg;
System.out.print(reg2 + "\t");
Pattern p = Pattern.compile(reg2);
Matcher m = p.matcher(str);
System.out.print(m.matches() + "\t");
Pattern p1 = Pattern.compile(reg2);
Matcher m1 = p.matcher(str);
boolean b;
while (b = m1.find()) {
System.out.print(b + "\t");
if (b) {
System.out.print(m1.group() + "\t");
System.out.print(m1.start() + "\t");
System.out.print(m1.end() + "\t");
}
}
System.out.print("\n");
}
调用f1():
f1("a{1,2}", "aaa");//
f1("a{1,2}?", "aaa");//
f1("a{1,2}+", "aaa");//
看到的现象(注意调用f1()的时候):
我试验了好几组正则表达式,每组中无论是贪婪量词,的样式懒惰量词,支配量词,使用matches()得到的要么都是true,要么都是FALSE。
问题是:1 。既然matcher.matches()函数要求的是“完全匹配”才返回true,那么与 贪婪量词,懒惰量词,支配量词没有关系?
2 。是不是贪婪量词,懒惰量词,支配量词仅仅与find()的返回值才有关系??
3.既然贪婪,和懒惰量词 :“贪婪”是从“吞入最多各字符”开始,找不到符合条件情况下,去掉一个字符,在找。。。。。
“懒惰”和贪婪正好相反,只要贪婪量词能找到匹配的,那么懒惰量词不也是也照样能找到吗???
4.既然支配量词是“全部匹配才行”
那么 "a{1,2}+"为什么能够从"aaa"中m.find()出来一个"aa"呢????;
5.
高手给我解解围呀!!!