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

user963263 picture user963263 · Sep 25, 2011 · Viewed 18k times · Source

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.

Answer

luobo25 picture luobo25 · Dec 13, 2011

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)