3

I am trying to validate some input where just latin letters are allowed. For that I am using the next regex:

public boolean isValidInput(String inputText) {
    return inputText != null && Pattern.matches("\\p{IsLatin}+", inputText);
}

But when I run it I get a PatternSyntaxException:

FATAL EXCEPTION: main
Process: com.test, PID: 27758
java.util.regex.PatternSyntaxException: U_ILLEGAL_ARGUMENT_ERROR
\p{IsLatin}+
    at java.util.regex.Pattern.compileImpl(Native Method)
    at java.util.regex.Pattern.compile(Pattern.java:411)
    at java.util.regex.Pattern.<init>(Pattern.java:394)
    at java.util.regex.Pattern.matches(Pattern.java:424)
    ...

I am using Java 8 and compiling with Android SDK 27. I have tested it in three devices with Android 6, 7 and 8 and same result.

I have tried to run the same code in an standalone Java 8 program and it works fine.
The Android regex doc page does not say anything that \p{IsLatin} is not supported.

Classes for Unicode scripts, blocks, categories and binary properties
\p{IsLatin} A Latin script character (script)

Any idea?

2
  • 1
    Interesting, you could try \\p{L} (letter) instead of \\p{IsL} (is latin). Maybe "(?u)\\p{IsLatin}+".
    – Joop Eggen
    Dec 21, 2017 at 10:25
  • \p{Latin} works! Thanks you so much Joop :) ..I've spent one day trying to make it work.. Dec 21, 2017 at 10:32

1 Answer 1

6

I've just noticed this line in the docs (thanks @Joop for pointing it out):

Categories may be specified with the optional prefix Is: Both \p{L} and \p{IsL} denote the category of Unicode letters.

It seems that \p{IsLatin} is just an alias of \p{Latin}. Both work in native Java, but just \p{Latin} works for Android (although Android Studio highlights it red and warns you that is an "Unknown character category").

So now my validation method looks like:

public boolean isLatinInput(String inputText) {
    return inputText != null && Pattern.matches("\\p{Latin}+", inputText);
}
1

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.