Regular Expression fails to matches() pattern with asterisk sign

后端 未结 2 1797
情话喂你
情话喂你 2021-01-29 10:09

The sample code:

    String test = \"Z\";
    Pattern pt = Pattern.compile(\"[0-9]*\");
    Matcher mc = pt.matcher(test);
    System.out.println(mc.find());
            


        
2条回答
  •  一生所求
    2021-01-29 10:51

    because Z is not in the class [0-9]*(This should matches only the digits between 0 to 9 check the demo regex), to match Z it should be in the class [0-9Z]* (demo regex)

    Pattern pt = Pattern.compile("[0-9Z]*");
    

提交回复
热议问题