matches&lookingAt&find
Pattern p=Pattern.compile("\\d");
Matcher m=p.matcher("a1b");
System.out.println(m.matches());//false 字符串"22bb23"是否完全匹配正则"\\d"
System.out.println(m.lookingAt());//false 字符串从开头是否可以匹配,"a"显然不匹配如果是"1ab"则为true,因为"1"在首位
System.out.println(m.find());//ture 字符串中是否可以匹配到
start&end&group
start
(匹配的起始位置) end
(匹配的结束位置) group
(匹配的值)必须在find
成功返回后调用,注意find
每次调用在字符串中的查询位置都会后退,如果已经全部查询完,则会报错
Pattern p=Pattern.compile("[a-z]");
Matcher m=p.matcher("a1b2");
System.out.printf("%b\t%s\t%d\t%d\n",m.find(),m.group(),m.start(),m.end());//true a 0 1
System.out.printf("%b\t%s\t%d\t%d\n",m.find(),m.group(),m.start(),m.end());//true b 2 3
System.out.printf("%b\t%s\t%d\t%d\n",m.find(),m.group(),m.start(),m.end());//java.lang.IllegalStateException: No match found
参考:
https://www.cnblogs.com/ggjucheng/p/3423731.html