Actions in Depth
Regular Expressions Support in Better Rename
Which regular expression syntax Better Rename understands, including case conversion in replacements with \U, \L, \u, \l and \E.
Better Rename allows you to use an advanced technique called “Regular Expression Substitution” to match patterns in file names and to rebuild or replace the matched parts.
This is an advanced technique which requires a certain familiarity with programming concepts and may thus prove inappropriate for novice computer users.
The feature is available via the Replace regular expression and the
Re-arrange using regular expression actions in
the Advanced & Special category. The help topics for either action contain a mini-tutorial on how to get started.
An overview of basic regular expression syntax is available in the Basic Regular Expression Syntax help topic.
Which action to use
Replace regular expressionreplaces every match of the pattern in the name and leaves everything else exactly as it was. Use it to strip, clean up or substitute parts of a name.Re-arrange using regular expressionrequires the pattern to match the whole name and builds the new name from the substitution alone. Use it to take a name apart into capture groups and put it back together in a different order. The preview table shows the contents of each capture group in its own column.
Capture groups
Parts of the pattern enclosed in brackets are “capture groups”. The text they matched is available in the
replacement or substitution as $1, $2, $3 and so on; $0 is the whole match. The older \1, \2, \3
syntax is accepted as well, so patterns written for other tools (or for very old versions of Better Rename)
can be pasted in unchanged.
Case conversion
You can change the case of matched text directly in the replacement or substitution using the escape sequences
familiar from sed and Perl:
\U— convert all following characters to uppercase until\E\L— convert all following characters to lowercase until\E\u— uppercase only the next character\l— lowercase only the next character\E— end any ongoing case conversion
Examples
- Replace
(.*)with\U$1\EmakesappleintoAPPLE - Replace
(\w+)with\u$1makesapple pieintoApple Pie(every word is a separate match) - Replace
(\w)(\w*)with\u$1\L$2\EmakeshELLO wORLDintoHello World
Case conversion requires the modern regular expression engine (see below).
Modern & legacy regular expression engines
Regular expression support has been a feature of the program for two decades, so there are many people who have saved presets and droplets that contain regular expressions written a long time ago.
Since version 12.19 the program contains two engines:
- The modern engine supports the full ICU syntax, adds the case
conversion escapes described above and accepts both the
$1and the\1capture syntax. - The legacy engine is the
RegexKitLitebased engine that earlier versions used. It exists only so that old presets and droplets keep producing exactly the names they always did.
The Replace regular expression action always uses the modern engine.
The Re-arrange using regular expression action has a Use legacy library checkbox. From version 12.34 onwards a
newly created action starts with this checkbox unticked, i.e. on the modern engine. Presets and droplets keep
the engine they were saved with: an action saved by a version before 12.19 runs on the legacy engine, and an
action saved by a later version remembers its own setting. You do not need to do anything to keep old presets
working.
Both engines understand the same pattern syntax, which is documented on the ICU website. The one difference in behaviour is described in the Re-arrange using regular expression help topic.
There are number of good regular expression tutorials available on the internet:
- Regular Expression Tutorial at regular-expressions.info
- regex101.com lets you test a pattern against sample text before using it on real files