7

Good day! I have a regex pattern :

 Pattern p = Pattern.compile("^[a-zA-Z_\\$][\\w\\$]*(?:\\.[a-zA-Z_\\$][\\w\\$]*)*$");

It should tell me if java / android package name is legal or not. It works fine on desktop java, but it failures on android devices

Lets say I have some package names :

 ". .", "ПАвыапЫВАПыва", "com.mxtech.ffmpeg.v7_neon", ...

Test should show that the only valid package is "com.mxtech.ffmpeg.v7_neon", but is also shows that test string

" _ПАвыапЫВАПыва\_ "

is valid. Why? (It is Cyrillic. )

What is the difference between Android and Desktop realizations?

1 Answer 1

9

The issue is caused by the fact that \w in Android regex is Unicode aware.

Replace with [A-Za-z0-9_] to only match ASCII letters, digits and an underscore.

See the Android Pattern reference:

Note that these built-in classes don't just cover the traditional ASCII range. For example, \w is equivalent to the character class [\p{Ll}\p{Lu}\p{Lt}\p{Lo}\p{Nd}]. If you actually want to match only ASCII characters, specify the explicit characters you want.

Sign up to request clarification or add additional context in comments.

1 Comment

Ah... Didn't know that.

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.