Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java regex error - Look-behind group does not have an obvious maximum length

I get this error:

java.util.regex.PatternSyntaxException: Look-behind group does not have an
    obvious maximum length near index 22
([a-z])(?!.*\1)(?<!\1.+)([a-z])(?!.*\2)(?<!\2.+)(.)(\3)(.)(\5)
                      ^

I'm trying to match COFFEE, but not BOBBEE.

I'm using java 1.6.

like image 431
user963263 Avatar asked Sep 25 '11 04:09

user963263


2 Answers

To avoid this error, you should replace + with a region like {0,10}:

([a-z])(?!.*\1)(?<!\1.{0,10})([a-z])(?!.*\2)(?<!\2.{0,10})(.)(\3)(.)(\5)
like image 70
luobo25 Avatar answered Oct 02 '22 08:10

luobo25


Java doesn't support variable length in look behind.
In this case, it seems you can easily ignore it (assuming your entire input is one word):

([a-z])(?!.*\1)([a-z])(?!.*\2)(.)(\3)(.)(\5)

Both lookbehinds do not add anything: the first asserts at least two characters where you only had one, and the second checks the second character is different from the first, which was already covered by (?!.*\1).

Working example: http://regexr.com?2up96

like image 13
Kobi Avatar answered Oct 02 '22 08:10

Kobi