My pattern swallows the whole line

Back to list2024-04-05
You

My pattern swallows the whole line.

ChatGPT

.* is greedy: it takes everything, then backtracks. Use .*? for the shortest match, or better, a negated class like [^"]* which cannot cross the delimiter at all and does not backtrack.

You
monthly-requests.png
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.

You

Anything that changes this at ten times the scale?

ChatGPT

At ten times the size the bottleneck moves from the operation itself to what it competes with: memory, locks and the people who have to run it. The approach stays, the batching gets smaller and the schedule matters more.