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

How to avoid catastrophic backtracking

Here are some tips to keep in mind while handling situations with catastrophic or excessive backtracking in your regex:

  • When you write regular expressions, make sure they fail fast without spending a lot of unnecessary steps in backtracking.
  • When using nested repetition operators or quantifiers, make sure that there is only one unique way to match the a string.
  • Make good judicious use of atomic groups and possessive quantifiers to avoid excessive backtracking.
  • You should avoid having too many optional matches that are not mutually exclusive in an alternation pattern.
  • Be very careful when using a free-flowing pattern such as .* or .+ in your regex. Wherever possible, use negated character classes for ...

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