R

TestRegex

← Back to Blog

Optimizing Performance: Avoiding Catastrophic Backtracking

Regex operations are usually fast, right? Until they aren't. A phenomenon known as "Catastrophic Backtracking" can cause a regex engine to take years to evaluate a short string.

The Dangerous Pattern

It typically happens with nested quantifiers, like: (x+x+)+y.

If you feed this pattern a string of "x"s that doesn't end in "y", the engine will try every possible combination of how to potential split the "x"s between the two internal groups. This grows exponentially.

3 Rules for Speed

  1. Anchor your patterns: Use ^ and $ whenever possible to limit scan scope.
  2. Be specific: Use [^"]* instead of .*. The dot is ambiguous; negated character classes are strict.
  3. Fail Fast: If your language supports it, use atomic grouping (?>...) or possessive quantifiers ++ to prevent backtracking.