具有占有量词的Java正则表达式不起作用?

时间:2012-11-30 12:33:35

标签: java regex expression

似乎我完全被阻止,因为我不知道为什么这段代码总是打印“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());   
    }

}

1 个答案:

答案 0 :(得分:2)

Matcher.find将对String执行部分匹配,因此始终会找到要匹配的数字。您需要使用Matcher.matches()来匹配完整的String

System.out.println(m.matches());