Monday, 20 May 2013

Java regex error - Look-behind with group reference

Java regex error - Look-behind with group reference

I'm trying to build a regex that matches exactly two occurrences of a char in a class. This is the regex I made:
(?<!\1)([^raol1c])\1(?!\1)
As you can see, it uses negatives look-ahead and behind but as usual the later does not work; java throws the well-known exception "look-behind group does not have an obvious maximum length" when it clearly have a maximum length (exactly one char).
Ideally the regex should match "hh", "jhh", "ahh", "hhj", "hha" but no "hhh".
Any ideas about how to deal with this and make a workaround?

No comments:

Post a Comment