If you are unfamiliar with regular expressions, please read the Regular Expressions Support in A Better Finder Rename help topic first.
This action provides classical regular expression search and replace. You provide a pattern, and every place in the file name where the pattern matches is replaced with the replacement you provide. Everything the pattern does not match is left exactly as it was.
In the simplest case this is exactly the same as replacing text, e.g.
Replace Hello with Good Bye in Hello World will result in Good Bye World.
Hello is a very simple regular expression made up of a series of characters. The power of regular expressions only
comes into play when you use “wildcard” characters for the matching.
. matches any character[a-z] matches any lower case alphabetical character[0-9] matches any digit (\d means the same)A simple regular expression for replacing all digits with an underscore would go like this:
Replace [0-9] with _ in Hello 101 will yield Hello ___.
By default any character matches itself and the wildcard characters we’ve just seen match precisely one character.
+ at the end of a character or wild card, this means “one or more”* at the end of a character or wild card, this means “zero or more”For instance, replace [0-9]+ with <NumberRemoved> in Hello 101 will yield Hello <NumberRemoved>.
In addition to the replacement capabilities that we have already seen, regular expressions also allow you to define
“capture groups”. These are sections of regular expressions that are enclosed in brackets, e.g. ([0-9]+) is a capture
group that matches a run of digits.
The advantage of using capture groups is that you can refer to them in the replacement term by using the dollar sign followed by the index of the capture group (you can have multiple capture groups).
An example might clarify this: let’s replace ([0-9]+) with <$1> in Hello 101; this will yield Hello <101>.
The capture group matches the 3 digit number which is then replaced with the capture group $1 which itself is enclosed
in angle brackets.
Replacing ([0-9]) with <$1> in Hello 101 will yield Hello <1><0><1> since the pattern now matches three
times, one digit at a time.
You can use multiple capture groups like this, e.g.
Replace ([a-z]+) ([0-9]+) with $2 $1 in Hello 101 (with Ignore case ticked) will yield 101 Hello.
Here the first capture group $1 matches the word at the beginning of the name and the second capture group
$2 matches the 3 digit number. Then we replace the entire matching section of the name (in this case the entire
name) with the second capture group ($2 = 101) followed by the first capture group ($1 = Hello).
The older \1, \2, \3 capture syntax is accepted as well, and the replacement can change the case of what it
inserts using \U, \L, \u, \l and \E, see Regular Expressions Support.
The action always replaces the specified regular expression (the “search term”) with the value you indicate (the “replacement term”) in the file name (the “input string”). The search term will match as much of the input string as possible, but will happily accept “partial” matches, e.g. where the whole input string does not match.
You can change this behaviour by using “anchors”, e.g.
^ matches the beginning of the file name / input string$ matches the end of the file name / input stringFor instance, . will match every character of the input string, but ^. will match only the first character and .$ only the last, e.g.
. with _ in Hello will yield _____.^. with _ in Hello will yield _ello..$ with _ in Hello will yield Hell_.If for instance, you want to swap the first and the second words separated by a space, but leave any file names that
consist of three or more words alone, you can use the search term ^([a-z]+) ([a-z]+)$, e.g.
^([a-z]+) ([a-z]+)$ with $2 $1 in Hello World will yield World Hello.^([a-z]+) ([a-z]+)$ with $2 $1 in Hello Again World will do nothing because there is a third term.^([a-z]+) ([a-z]+)$ with $2 $1 in Hello Again 101 will do nothing because there is a third term.Without the anchors, this would happen:
([a-z]+) ([a-z]+) with $2 $1 in Hello World will yield World Hello.([a-z]+) ([a-z]+) with $2 $1 in Hello Again World will yield Again Hello World because the search term
matches the first two words and the third is left in place.([a-z]+) ([a-z]+) with $2 $1 in Hello Again 101 will yield Again Hello 101.A few details worth knowing:
Ignore case is ticked by default, so img_ matches IMG_. Untick it when the case is what you are matching on.x* that can match “nothing” does not insert the
replacement between every character.If your goal is to re-arrange existing input strings, you may find the Re-arrange using regular expression action useful.
In that action, the search term is automatically “anchored” (i.e. enclosed in ^ and $), the new name is built
from the substitution alone, and the capture groups are displayed in the preview.
More details about regular expression syntax are available here.