Regex that is slow on long input

Back to list2025-09-04
You

A pattern hangs on certain inputs.

ChatGPT

Catastrophic backtracking, usually from nested quantifiers like (a+)+. Rewrite so each position has one way to match, or use a library with a linear-time engine. Input length alone rarely causes it; ambiguity does.

You

My regex for nested brackets keeps failing.

ChatGPT

Because regular expressions cannot count. Nesting needs a parser, even a ten line one with a stack. The regex that appears to work handles two levels and fails on the third, which is worse than not working at all.