6

I'm having trouble with a regular expression when targeting the Android platform 2.2.3.

The following regular expression works when targeting the Java VM on my desktop and the regular expression is also working on a .NET application too.

Pattern.compile("\\b?")

But when I target my phone I get a PatternSyntaxException. Any ideas?

donfuxx
  • 11,277
  • 6
  • 44
  • 76
Tentux
  • 714
  • 1
  • 4
  • 13
  • Don't know motivation of the error... but this is an optional (and however zero length) word boundary... what are you going to match after all?? – Gabber Oct 03 '12 at 22:25
  • Well it's a rather complicated regex from a different project. But I don't see why an optional word boundary should be invalid pattern syntax. – Tentux Oct 03 '12 at 23:44

1 Answers1

4

I can confirm that this does throw a PatternSyntaxException when running in the Android emulator, but not in a regular Java application. I can't see why that would be the case, other than the fact that regular expression implementation used in Android is different than in the normal Java SDK. From the Pattern Android Developers page:

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

As a work-around, I did discover that you can get around the exception by enclosing the word boundary assertion in a non-capturing group.

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

(A capturing group works as well, but I doubt you need it.)

I suggest you report this as a bug to see if you can get an official response. (I already searched, and it doesn't appear to be reported yet.)

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
  • 1
    But be aware that it's a bug only in the sense that Android rejects some regexes that the reference implementation accepts. It makes absolutely no sense to include a zero-width assertion and then make it optional. You're saying, *this condition must hold true, but it's okay if it doesn't.* By rejecting it, compiler is alerting you to what is either a typo or a failure of understanding on the author's part. – Alan Moore Oct 04 '12 at 00:55
  • 1
    @AlanMoore I was trying to imagine what the rest of the OP's regex might look like to need an optional word boundary, but I think you're right. It doesn't make sense. The way you make a word boundary optional is to just not include it in the regex at all. – Bill the Lizard Oct 04 '12 at 01:58
  • @BilltheLizard Agreed. I don't think it does make sense. I'm not the author of the original regex. Perhaps I misunderstood their usage of '?' could be part of a capture group possibly. Anyway, I've decided to just implement the same solution my way. – Tentux Oct 05 '12 at 14:53