How do I use the IsAlphabetic binary property in a Java regex match?

前端 未结 1 1191
醉酒成梦
醉酒成梦 2021-01-25 03:51

I\'m using this pattern to check if a string starts with at least 2 alphabetic characters in front a colon:

string.matches(\"^\\\\p{IsAlphabetic}{2,}:\")
         


        
相关标签:
1条回答
  • 2021-01-25 04:52

    Works and returns true using java 1.8.

    String s = "äö:";
    System.out.println(s.matches("^\\p{IsAlphanumeric}{2,}:"));
    

    Note that the forms available in Java 1.7 - Alpha, IsAlpha - do not necessarily include characters not in US-ASCII . This returns false:

    String s = "äö:";
    System.out.println(s.matches("^\\p{IsAlpha}{2,}:"));
    

    But note that this works in 1.7 and returns true:

    String s = "äö:";
    Pattern pat = Pattern.compile( "^\\p{Alpha}{2,}:",
                         Pattern.UNICODE_CHARACTER_CLASS );
    Matcher mat = pat.matcher( s );
    System.out.println(mat.matches());
    
    0 讨论(0)
提交回复
热议问题