matcher0对象在调用matches()后修改了这个对象的某个全局变量.
在match()方法中有this.oldLast = this.last;等代码, 而find()中则没有.
这个就是match()和find()方法的区别
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* mathch()和find()方法的区别
* @author Administrator
*
*/
public class TestReg{
public static void main(String[] args) {
Pattern p = Pattern.compile("^[0-5]*$");
Matcher mach = p.matcher("2131232131");
if(mach.find()){
System.out.println("fin(): " + mach.group());
}else{
System.out.println("none find");
}
System.out.println("*************************");
if(mach.matches()){
System.out.println("match(): " + mach.group());
}else{
System.out.println("none matched");
}
}
}
你可以把find放前面,matches放后面。
因为matches中有一行代码:
this.oldLast = this.last;
相当于把游标放到了最后了。这个时候再用find就找不到了。