System.out.println("11".matches("[10-12]"));
为什么输出false
我想匹配10、11、12的表达式应该如何写?

解决方案 »

  1.   

    System.out.println("11".matches("1[0-2]"));试试呢
      

  2.   

    1[012]public class Test {
    public static void main(String[] args) {
    System.out.println("10".matches("1[012]"));
    System.out.println("11".matches("1[012]"));
    System.out.println("12".matches("1[012]"));
    System.out.println("13".matches("1[012]"));
    System.out.println("9".matches("1[012]"));
    }
    }
      

  3.   


    你的可以正确匹配10 11 12
    但能解释下为什么System.out.println("11".matches("[10-12]"));这个不行么?
      

  4.   

    你在写的时候需要指出要匹配的数据类型,System.out.println("11".matches("\\d[10-12]"));这样写就对了,\d数字
    . 任何字符(与行结束符可能匹配也可能不匹配) 
    \d 数字:[0-9] 
    \D 非数字: [^0-9] 
    \s 空白字符:[ \t\n\x0B\f\r] 
    \S 非空白字符:[^\s] 
    \w 单词字符:[a-zA-Z_0-9] 
    \W 非单词字符:[^\w]