Matcher: find vs matches

来源:互联网 发布:php 伪静态 编辑:程序博客网 时间:2024/04/27 22:00
/*matches tries to match the expression against the entire string and implicitly add a ^ at the start and $ at the end of your pattern, meaning it will not look for a substring. Hence the output of this code:*/public static void main(String[] args) throws ParseException {    Pattern p = Pattern.compile("\\d\\d\\d");    Matcher m = p.matcher("a123b");    System.out.println(m.find());    System.out.println(m.matches());    p = Pattern.compile("^\\d\\d\\d$");    m = p.matcher("123");    System.out.println(m.find());    System.out.println(m.matches());}// matches:判断整个字符串是否匹配, lookingAt判断字符串的起始部分(不必是整个字符串)/* output:truefalsetruetrue*/

0 0