Actions in Depth
The “Re-arrange using regular expression” Action
Re-arrange file name parts with a regular expression in A Better Finder Rename: capture groups, $1 $2 substitution, per-group preview columns, legacy engine.
If you are unfamiliar with regular expressions, please read the Regular Expressions Support in A Better Finder Rename help topic first.
This action is specifically intended to allow you to easily re-arrange parts of existing file names using the power of regular expressions to match the different file name parts.
You specify a “pattern” as a regular expression with capture groups and then use the capture variables $1, $2, $3
in the “substitution expression” to re-arrange the different parts of the name. A Better Finder Rename shows the
contents of each capture variable in its own column of the preview table to help you tweak your settings.
Capture groups and capture variables are explained in more detail in the Basic Regular Expression Syntax help topic.
How it works
Two rules set this action apart from the Replace regular expression action:
- The pattern must match the whole file name. The pattern is automatically “anchored”, as if it were enclosed
in
^and$. If it does not match the entire name, the name is left unchanged and the capture columns in the preview stay empty. You never need to type the anchors yourself, but it does no harm if you do. - The new name is built from the substitution alone. Nothing that the pattern did not capture survives into the new name, so the substitution has to contain everything you want to keep.
As a simple example of using this feature, let’s swap the first and the second word of a couple of two word file names.
We start by defining a regular expression with two capture groups, e.g. (.*) (.*) defines two capture groups with
arbitrary content separated by a space.
The substitution expression is a string in which we can substitute the variables $1, $2, $3 with the content of
the capture group with the same index.
$2 $1 will simply swap the contents of the first and the second capture groups.
hello world→world helloJohn Smith→Smith John
Adding arbitrary text around the capture variables simply includes this text in the new file name, e.g.
Dear Mr $2 in the above John Smith example would yield Dear Mr Smith.
Because the pattern must match the whole name, a partial pattern does nothing:
- Pattern
(\d+)with substitutionHawaii_$1leavesIMG_4821unchanged, becauseIMG_is not matched - Pattern
IMG_(\d+)with substitutionHawaii_$1turnsIMG_4821intoHawaii_4821
To pick something out of the middle of a name, let the pattern swallow the parts you do not want:
- Pattern
.*?(INV-\d+).*with substitution$1turnsScan of INV-20419 finalintoINV-20419
The action supports capture variables $1 to $8; $0 holds the whole name. The older \1, \2, \3 syntax is
accepted as well.
The Use legacy library checkbox
Since version 12.19 the action can run on two engines, see Regular Expressions Support.
- A newly created action starts on the modern engine (checkbox unticked, from version 12.34 onwards)
This is the engine that supports case conversion (
\U,\L,\u,\l). - An action loaded from a preset or droplet saved by a version before 12.19 runs on the legacy engine (checkbox ticked). Actions saved by later versions remember their own setting.
The only difference in behaviour between the two engines is that the legacy engine is not anchored: it uses the
first place in the name where the pattern matches and discards the rest of the name. With the (\d+) example above
the legacy engine would produce Hawaii_4821 where the modern engine leaves the name alone. Old presets that rely on
this will keep working as long as the checkbox stays ticked; when you write a new pattern, make it match the whole
name instead.