2

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)

15
  • it works for me. Note that matches method tries to match the whole string. Commented Apr 18, 2015 at 14:35
  • It doesn't matter if I enclose \\p{IsAlphabetic} in a character class with []. Commented Apr 18, 2015 at 14:36
  • @Avinash: what Java version and OS are you on? Commented Apr 18, 2015 at 14:37
  • What Java version are you using? The pattern works in 1.8. Commented Apr 18, 2015 at 14:37
  • Your pattern works correctly in this online tester: regexplanet.com/advanced/java/index.html. Commented Apr 18, 2015 at 14:39

1 Answer 1

3

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());
Sign up to request clarification or add additional context in comments.

1 Comment

The form with \p{Alpha} (but not the one with \p{IsAlpha}) does work if the UNICODE_CHARACTER_CLASS flag is specified for the pattern, as documented. Tested on Java 8 against the input given in this answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.