Optional text border expression for Android

I'm having regex issues when targeting Android 2.2.3.

The following regular expression works when targeting the Java virtual machine on my desktop, and the regular expression also works with a .NET application.

Pattern.compile("\\b?") 

But when I aim at my phone, I get a PatternSyntaxException. Any ideas?

+6
source share
1 answer

I can confirm that this PatternSyntaxException when launched in the Android emulator, but not in a regular Java application. I don’t understand why this is so, except for the fact that the implementation of the regular expression used in Android is different from the regular Java SDK. On the Android Developers Pattern page:

The regex implementation used in Android is provided by the ICU. The notation for regular expressions is basically a superset of those used in other implementations of the Java language. This means that existing applications usually work as expected, but in rare cases, Android may take a regular expression that is not accepted by other implementations.

As a job, I found that you can get around the exception by including the word boundary statement in a non-capture group.

 Pattern.compile("(?:\\b)?"); 

(The capture group works as well, but I doubt you need it.)

I suggest that you report this as a bug to find out if you can get an official response. (I’ve already searched, and it hasn’t been reported yet.)

+4
source

Source: https://habr.com/ru/post/926893/


All Articles