If you need to rename a number of files in complicated ways, RenameRegEx can do it.

RenameRegEx adds a shortcut to the Windows Explorer context menu. When you select two or more files and right-click on them, you can select the Rename RegEx… command to display the RenameRegEx window. There you can enter the pattern to match and the replacement pattern, using the full power of regular expressions.

main window

In the target expression, you can use $1 through $9 for groups you matched in the source expression.

You can use the right-arrows after each expression field to enter any of the special characters described below. This is usually easier than memorizing the sometimes arcane syntax of regular expressions.

The dropdown combo boxes store their previous values so you can easily reuse previous expressions.

The Case-sensitive matching check box enables, of course, case-sensitive searches, which ensures that the text will only match if the case matches.

If you select the Replace all matches check box, every match will be replaced with the target expression. If the check box is cleared, only the first match will be replaced.

If the Replace "#" with… check box is selected, any occurrence of the # character in the target expression will be replaced by a number, starting with the number you enter.

Regular Expressions

This documentation will be fairly brief and it assumes you have some knowledge of regular expressions. If you want to learn more, you can read about them on the Internet.

You use a regular expression to match a specific pattern of text, such as all words that begin with “b” or all text starting with 0 and ending with a period.

Regular Expression Syntax

.Matches any single character.
[ ]Indicates a character class. Matches any character inside the brackets (for example, [abc] matches "a", "b", and "c").
^If this metacharacter occurs at the start of a character class, it negates the character class. A negated character class matches any character except those inside the brackets (for example, [^abc] matches all characters except "a", "b", and "c").
If ^ is at the beginning of the regular expression, it matches the beginning of the input (for example, ^[abc] will only match input that begins with "a", "b", or "c").
-In a character class, indicates a range of characters (for example, [0-9] matches any of the digits "0" through "9").
?Indicates that the preceding expression is optional: it matches once or not at all (for example, [0-9][0-9]? matches "2" and "12").
+Indicates that the preceding expression matches one or more times (for example, [0-9]+ matches "1", "13", "666", and so on).
*Indicates that the preceding expression matches zero or more times.
??, +?, *?Non-greedy versions of ?, +, and *. These match as little as possible, unlike the greedy versions which match as much as possible. Example: given the input "", <.*?> matches "" while <.*> matches "".
( )Grouping operator. Example: (\d+,)*\d+ matches a list of numbers separated by commas (such as "1" or "1,23,456").
{ }Indicates a match group. The actual text in the input that matches the expression inside the braces can be retrieved through the CAtlREMatchContext object.
\Escape character: interpret the next character literally (for example, [0-9]+ matches one or more digits, but [0-9]\+ matches a digit followed by a plus character). Also used for abbreviations (such as \a for any alphanumeric character; see table below).
If \ is followed by a number n, it matches the nth match group (starting from 0). Example: <{.*?}>.*?</\0> matches "<head>Contents</head>".
$At the end of a regular expression, this character matches the end of the input. Example: [0-9]$ matches a digit at the end of the input.
|Alternation operator: separates two expressions, exactly one of which matches (for example, T|the matches "The" or "the").
!Negation operator: the expression following ! does not match the input. Example: a!b matches "a" not followed by "b".

Abbreviations

You can also use abbreviations, such as \d instead of [0-9].

\aAny alphanumeric character.
\bWhite space (blank).
\cAny alphabetic character.
\dAny decimal digit.
\hAny hexadecimal digit.
\nNewline.
\qA quoted string.
\wA simple word.
\zAn integer.

Tips and Tricks

If you have a set of numbered files that you want to renumber, it’s possible that the filenames might “collide” during renaming. For example:

Current name New name
test1.txt test1.txt
test11.txt test12.txt
test2.txt test3.txt

If the source expression is test.+ and the target expression is test#.txt, RenameRegEx will display an error when it tries to rename test11.txt to test2.txt because test2.txt already exists.

The workaround is to modify the rest of the filename too, not just the numbers. In this example, you can change the target expression to test_#.txt. (Note the new underscore.)

Note: I've seen times—usually when I've selected dozens of folders, shortcuts, and files—when Windows has given Rename Regular Expression invalid locations for its menu command. In these cases, RenameRegEx will merely not add its command to the context menu. You can select fewer items and try again.

Requirements

RenameRegEx runs on Microsoft® Windows® Vista, XP Home and Professional, 2003, and 2000. (Although it may work on other versions of Windows, it’s not guaranteed.)

History

1.10: Added support for folders, shortcuts, etc. Fixed some cases where the context menu command wouldn't appear.