O'Reilly logo

Java 9 Regular Expressions by Anubhava Srivastava

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required