Multi-Rename, regex: can be slow

Discussion related to "Everything" 1.5 Alpha.
Post Reply
therube
Posts: 4977
Joined: Thu Sep 03, 2009 6:48 pm

Multi-Rename, regex: can be slow

Post by therube »

just noting...


regex: (in multi-rename dialog)

from: (.*)sal-...(-\d\d\d.*)
to: (.*)+sal-...(-\d\d\d.*)

is rather (can be rather) slow

(as in, going back, after the fact, & adding the '+',
i thought i actually mis-typed, as i did not see any
change in my New Filenames: (as i should have)

(only to wait a moment, &, oh, there they are)
NotNull
Posts: 5461
Joined: Wed May 24, 2017 9:22 pm

Re: Multi-Rename, regex: can be slow

Post by NotNull »

Does it help when you add an 'anchor', like:

^(.*)sal-...(-\d\d\d.*)
or
(.*)sal-...(-\d\d\d.*)$
or even
^(.*)sal-...(-\d\d\d.*)$
raccoon
Posts: 1017
Joined: Thu Oct 18, 2018 1:24 am

Re: Multi-Rename, regex: can be slow

Post by raccoon »

You can speed up almost every regex that uses .* by using .*? instead. That is because .* performs a full-gobble expansion and then backtracks from the end to the beginning again. Your example has two (nested) .* and so it performs this full gobble expansion n-squared times. Most human beings do not intend this behavior when writing their patterns. They expect and anticipate a forward march to reach their intended goal, and usually, it would reach the goal much faster when using a forward march. (.*?)

So use .*? by default and do not use .* if you have any doubts or slowness issues. .* has its intended uses, but they are rare.

And yes, you can't possibly want (.*)+ in this pattern. You certainly mean (.*?) or (.)+?
Post Reply