find和matches 正则匹配的结果不一样。
String content = "13 1.23_mongodb ";
String regex = "^\\d+\\s{2,}";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(content);
System.out.println("find = "+m.find());
System.out.println("matches = "+Pattern.matches(regex, content));
结果:
find = true
matches = false
String content = "13 1.23_mongodb ";
String regex = "^\\d+\\s{2,}.*";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(content);
System.out.println("find = "+m.find());
System.out.println("matches = "+Pattern.matches(regex, content));
结果:
find = true
matches = true
原因:
Pattern.matches 为全匹配。
find 为分段匹配