In a java regex, how can I get a character class e.g. [a-z] to match a - minus sign?

后端 未结 5 765
庸人自扰
庸人自扰 2020-12-06 10:19
Pattern pattern = Pattern.compile(\"^[a-z]+$\");
String string = \"abc-def\";
assertTrue( pattern.matcher(string).matches() ); // obviously fails

I

5条回答
  •  感情败类
    2020-12-06 10:40

    This works for me

       Pattern p = Pattern.compile("^[a-z\\-]+$");
       String line = "abc-def";
       Matcher matcher = p.matcher(line);
       System.out.println(matcher.matches());  // true
    

提交回复
热议问题