似乎我完全被阻止,因为我不知道为什么这段代码总是打印“true”:
public class Main {
public static void main(String[] args) {
Pattern p = Pattern.compile("[1-9]{1,2}");
Matcher m = p.matcher("1234567");
System.out.println(m.find());
}
}
答案 0 :(得分:2)
Matcher.find
将对String
执行部分匹配,因此始终会找到要匹配的数字。您需要使用Matcher.matches()
来匹配完整的String
:
System.out.println(m.matches());