Non-greedy catch-all in Tcls NFA seems to have quadratic time complexity (O(n**2)
),
see the comparision (greedy vs. non-greedy) for 1st and 2nd sub-REs (.*)
and (.*?)
below.
Explanation: a growth of n
(length of matched part, e. g. from 500 to 5000 chars) by a factor of 10
increases the time of evaluation by a factor of 100 (452µs vs 40598µs).
Note that the whole RE is anchored from both sides, so the greedyness even doesn't really matter here (the result of match will be the same in any case).
The 3rd RE is added for the illustration, that NFA seems not even consider the next RE (\s+
which shall definitely match)
in case of slow non-greedines, unless added explicitely, like in (.*?(?=\s))
, what then immedially switches to linear complexity O(n)
.