buy batch renamer

Buy From Us for $29.95

30 Day No-Hassle Money Back Guarantee, all purchases include discounted upgrades and we offer site and forever licenses.

Upgrade

Purchase an upgrade to version 12.

download Vitamin-R

Download A Better Finder Rename 12.05

for Intel & Apple Silicon Macs, requires macOS 10.13 or later.

Download A Better Finder Rename 11.53

for Intel & Apple Silicon Macs, requires macOS 10.10 or later.

Download A Better Finder Rename 12.05

for Intel & Apple Silicon Macs, requires macOS 10.15 or later.

The “Replace regular expression” Action

If you are unfamiliar with regular expressions, please the Regular Expressions Support in A Better Finder Rename help topic first.

This action provides classical regular expression support. You provide a pattern and it is replaced in the target string with the value that you provide.

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

A simple regular expression for replacing all numbers with an underscore would go like this:

Replace [0-9] with _ for Hello 101 this will yield Hello ___.

By default any character matches itself and the wildcard characters we’ve just seen match precisely one character.

  • if we add a + at the end of a character or wild card, this means “one or more”
  • if we add a * 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 <Number Removed>.

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 all digits in a string.

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 each capture group now matches only a single character.

You can use multiple capture groups like this, e.g.

Replace ([a-z]*) ([0-9]*) with $2 $1 in Hello 101 will yield 101 World.

Here the first capture group $1 matches the characters at the beginning of the string and the second capture group $2 matches the 3 digit number. Then we replace the entire matching section of the file (in this case the entire string) with the second capture group ($2 = 101) followed by the first capture group ($1 = Hello).

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 string

For instance, . will match every character of the input string, but ^. will match only the first character and .$ only the last, e.g.

  • Replace . with _ in Hello will yield _____.
  • Replace ^. with _ in Hello will yield _ello.
  • Replace .$ 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.

  • Replace ^([a-z]*) ([a-z]*)$ with $2 $1 in Hello World will yield World Hello.
  • Replace ^([a-z]*) ([a-z]*)$ with $2 $1 in Hello Again World will do nothing because there is a third term.
  • Replace ^([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:

  • Replace ([a-z]*) ([a-z]*) with $2 $1 in Hello World will yield World Hello.
  • Replace ([a-z]*) ([a-z]*) with $2 $1 in Hello Again World will yield Again Hello World because the search term will match as much of the file name as it can
  • Replace ([a-z]*) ([a-z]*) with $2 $1 in Hello Again 101 will yield Again Hello 101.

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” (e.g. enclosed in ^ and $) and the capture groups are displayed in the preview.

More details about regular expression syntax is available here.

[Back to Help Topics]