After reading a lot how to optimize my regex, I discovered ?>
which supposedly helps preventing unnecessary backtracking.
So I have this regex:
aaa(?>.{1,5000}?bbb).{1,5000}?(?=ccc|$)
For some reason, when I remove the ?>
(regex below) I get no more backtracking error limit reached.
aaa.{1,5000}?bbb.{1,5000}?(?=ccc|$)
How is it possible? If the error happened without ?>
I could understand why, but I cant understand how using it, causes the backtrack error.
The atomic group prevents backtracking into itself, and forces the next lazy pattern (.{1,5000}?) to make possibly an enormous number of successive, tiny steps from a fixed (and potentially "bad") position. The non-atomic alternative allows backtracking so that it can reposition the bbb match economically, sometimes pruning the search more quickly and not reaching the total step limit, although it does technically involve backtracking steps.