Lookbehind limitations in Java regular expressions

Like many other regular expression engines, the Java regex engine does not allow a variable-length text without an obvious maximum length match in the lookbehind regex pattern. This means that we cannot use the * or + quantifiers in a lookbehind pattern. However, the Java regex engine allows for limited or finite repetition in a lookbehind regex. This gives us a workaround for doing so in Java regular expressions by using limiting quantifiers in lookbehind expressions.

This means that we cannot use the following lookbehind regex to check filenames ending with extensions:

(?<=\w\.\w+)$ 

However, we could change the preceding pattern to the following with a finite repetition, and now this ...

Get Java 9 Regular Expressions now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.