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

Use lazy quantifiers strategically instead of greedy quantifiers that cause excessive backtracking

Suppose we need to match an input that contains three complete words, start, middle, and end, separated by non-whitespace characters.

Consider using the following pattern with a lazy quantifier:

\bstart\b\S+?\bmiddle\b\S+?\bend\b 

Instead of using the following pattern, our match will be faster if we use the preceding pattern:

\bstart\b\S+\bmiddle\b\S+\bend\b 

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