Regex always does its best to fit. The only thing you do in this case is to slow down your parser by returning it back to /o*?/ Node. Once for each 'o' in "tooooon" . Whereas in normal matching the first time you need as many 'o' s as possible. Since the next element to match is 'n' , which will not match 'o' , it makes little sense to try to use minimal matching. In fact, when a normal match fails, it takes quite a while to complete. It should return every 'o' until there is nothing left to retreat. In this case, I would use the maximum matching /to*+n/ . 'o' would take everything he could and never give it back. This would make it so that when he fails, he will work quickly.
Minimum RE:
'toooooon' ~~ / to *? n /
toooooon
{t} match [t]
[t] match [o] 0 times
[t] <n> fail to match [n] -> retry [o]
[t] {o} match [o] 1 times
[t] [o] <n> fail to match [n] -> retry [o]
[t] [o] {o} match [o] 2 times
[t] [o] [o] <n> fail to match [n] -> retry [o]
. . . .
[t] [o] [o] [o] [o] {o} match [o] 5 times
[t] [o] [o] [o] [o] [o] <n> fail to match [n] -> retry [o]
[t] [o] [o] [o] [o] [o] {o} match [o] 6 times
[t] [o] [o] [o] [o] [o] [o] {n} match [n]
Normal RE:
(NOTE: similar to Maximal RE)
'toooooon' ~~ / to * n /
toooooon
{t} match [t]
[t] {o} {o} {o} {o} {o} {o} match [o] 6 times
[t] [o] [o] [o] [o] [o] [o] {n} match [n]
Minimum RE Failure:
'toooooo' ~~ / to *? n /
toooooo
. . . .
. . . .
[t] [o] [o] [o] [o] {o} match [o] 5 times
[t] [o] [o] [o] [o] [o] <n> fail to match [n] -> retry [o]
[t] [o] [o] [o] [o] [o] {o} match [o] 6 times
[t] [o] [o] [o] [o] [o] [o] <n> fail to match [n] -> retry [o]
[t] [o] [o] [o] [o] [o] [o] <o> fail to match [o] 7 times -> match failed
Failure of normal RE:
'toooooo' ~~ / to * n /
toooooo
{t} match [t]
[t] {o} {o} {o} {o} {o} {o} match [o] 6 times
[t] [o] [o] [o] [o] [o] [o] <n> fail to match [n] -> retry [o]
[t] [o] [o] [o] [o] [o] match [o] 5 times
[t] [o] [o] [o] [o] [o] <n> fail to match [n] -> retry [o]
. . . .
[t] [o] match [o] 1 times
[t] [o] <o> fail to match [n] -> retry [o]
[t] match [o] 0 times
[t] <n> fail to match [n] -> match failed
Maximum RE Failure:
'toooooo' ~~ / to * + n /
toooooo
{t} match [t]
[t] {o} {o} {o} {o} {o} {o} match [o] 6 times
[t] [o] [o] [o] [o] [o] [o] <n> fail to match [n] -> match failed
Brad gilbert
source share