How do I use the IsAlphabetic binary property in a

2020-04-30 03:25发布

问题:

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,}:")

but I get the following exception thrown at me:

java.util.regex.PatternSyntaxException: Unknown character property name {Alphabetic} near index 16
    ^\p{IsAlphabetic}{2,}:
    ^
    at java.util.regex.Pattern.error(Pattern.java:1730)
    at java.util.regex.Pattern.charPropertyNodeFor(Pattern.java:2454)
    at java.util.regex.Pattern.family(Pattern.java:2429)
    at java.util.regex.Pattern.sequence(Pattern.java:1848)
    at java.util.regex.Pattern.expr(Pattern.java:1769)
    at java.util.regex.Pattern.compile(Pattern.java:1477)
    at java.util.regex.Pattern.<init>(Pattern.java:1150)
    at java.util.regex.Pattern.compile(Pattern.java:840)
    at java.util.regex.Pattern.matches(Pattern.java:945)
    at java.lang.String.matches(String.java:2102)

even though the specification of the Pattern classes states:

Binary properties are specified with the prefix Is, as in IsAlphabetic. The supported binary properties by Pattern are

  • Alphabetic
  • Ideographic
  • Letter
  • ...

and the section Classes for Unicode scripts, blocks, categories and binary properties lists

\p{IsAlphabetic} An alphabetic character (binary property)

回答1:

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());