Page 1 of 1

Regular expression syntax ?

PostPosted: Tue Dec 14, 2010 4:05 am
by simonfbarnes
I can't find anything on the site that describes the syntax for using regular expressions in renaming. I know there is an example in the context menu: {FileName}{RegEx:(_)?(.*?)(_)?([0-9]+)?$}{3}{4}{1}{2} which reorders the parts of the name, but this still leaves one to guess that "RegEx: applies to the filename (it seems not)

Re: Regular expression syntax ?

PostPosted: Sun Dec 19, 2010 4:28 am
by Axel
The program use standard ECMAScript regular expressions as part of the most advanced options of its built-in file Rename function (F2).

The example in the context menu transforms file names in the form _ABC1234.jpg into 1234_ABC.jpg, something impossibe to do with standard renaming. The {RegEx:...} part defines the various captures (i.e. splits the file name down to various components), the rest, {3}{4}{1}{2}, specifies the how the resulting name should be built from the various captured components.

You can insert any other substitution variable or litteral parts in the template, say

Code: Select all

will transform "_ABC1234" into "MyImage#1234-HasSuffix-ABC".

The example regular expression "(_)?(.*?)(_)?([0-9]+)?$" splits the string "_ABC1234" into

0: (_ABC1234)
1: (_)
2: (ABC)
3: ()
4: (1234)

It also splits "ABC_1234" into

0: (ABC_1234)
1: ()
2: (ABC)
3: (_)
4: (1234)

{3}{4}{1}{2} then gives 1234_ABC and _1234ABC, respectively. These are just sample illustrating some manipulations.

Once you have your capture groups set up, it's just a matter of re-assembling the parts as you see fit.

Regular expressions are very powerful and let users create very complex renaming schemes, but the syntax is a bit awkward to say the least... Full documentation here:

You don't need the {FileName} part in the template for this to work. The {FileName} variable simply represents the original file name, so you can write "My file is named {FileName}" and this will translate to ""My file is named _ABC1234" as you'd expect.

For readers stumbling on this and wondering what we are talking about, FastPictureViewer Professional supports renaming templates where users can define rename "presets" and apply them quickly with the F2 function key, the standard Windows rename shortcut.

By default the renaming function works as anyone expects: press F2, edit the name, press Enter and you are done. In addition to this standard functionality, the program let users create presets that go far beyond this, for example it is possible to automatically insert the current date or some EXIF information in the new file name, using simple substitution variables like for example "{ISOSpeed}", which will be replaced by a number representing the camera ISO setting. The program also support advanced renaming features involving sophisticated techniques described above.

We have a renaming tutorial online on our help page at

EDIT: while playing with the renaming template editor I noticed that it did not save the template being edited, in some circumstances. The issue was fixed and the new installers ( should correct this occasional problem.

Re: Regular expression syntax ?

PostPosted: Thu Jan 19, 2012 6:37 pm
by jlindsey
WOW Axel that was helpful. I'm seeing that regular expressions are really powerful, but I grew up with assembler and pascal. Anyway I was just hunting for pulling the last 2 or three characters from the file-name, but I'm guessing what I got will work fine. Maybe a few more "Simple" examples would be helpful. Thanks


Re: Regular expression syntax ?

PostPosted: Fri Jan 20, 2012 6:10 am
by Axel
selecting the last N, say 3, characters of a filename should be easy

You want to skip zero or more occurences of any any leading characters, so start with


Then you want to capture 3 characters so you start a capture group () with 3 dots inside


then you can add a $ sign to tell the group should be at the end.

Code: Select all

Code: Select all
Code: Select all

...will capture the last 3 characters in a group (group#1) and said group is then pasted into the new name.

There are probably a number of other ways. The above will not match 1 and 2 characters file name, only 3 and up, and the last 3 characters will be captured.

You can find good info on regular expressions on the following site:

Have fun!

PS: from your example I see that I could add a couple of extra "date time" formatting presets, like ISO 8601

Re: Regular expression syntax ?

PostPosted: Tue May 28, 2013 11:22 pm
by tstoddard
This might be a dumb question but I'm going to ask it anyway.

Is it possible to use a target for a regular expression other than the filename in the renaming template editor? For example, if I want to return the first 4 characters of the CameraModel, can I do that? If so, do I specify the target of the regular expression when using one in a naming template.

For example, if {RegEx:$(....)} returns the first 4 characters of the target, how do I tell the regular expression to look at the string returned by the CameraModel macro and not the filename?

Re: Regular expression syntax ?

PostPosted: Wed May 29, 2013 12:12 am
by Axel
The {RegEx} and {RegEx*} macros' source is the file name and the full path name, respectively. There is currently no mechanism to apply a regular expression to other fields. Read below!

Re: Regular expression syntax ?

PostPosted: Sat Apr 09, 2016 7:05 am
by Axel
v1.9.356.0 introduces a generalization of the regular expressions that can now be applied to any variable, and a new {FilePath[n]} option allowing to split a path into its components as well as a number of string manipulation functions that can all be composed together.

Let's start from the new option for path components: some users have expressed the need to parse the path (not just the filename) to extract the name of the parent or grandparent folder, for example, and use it / them as component of the new file names they are creating.

The RegEx* macro was originally introduced to that effect (see above) and made it possible to split and capture path components, for example:

c:\images\birds\ducks\img1.jpg can be renamed to birds_ducks_img1.jpg using for example:

Code: Select all
So far so good but the expression is a bit cryptic, so v1.9.356 introduced the simpler to use {FilePath[n]} option, where n is between 0 and 9, and where 0 means the file name part, 1 the parent folder, 2 the grandparent folder etc, so you can reach up to 8 levels above the current folder you are in.

You get the same result as the above {RegEx*} with:

Code: Select all
which is a lot easier to read. Note that {FileName} is now equivalent to {FilePath[0]}, both can be used interchangeably.

Version 1.9.356 also generalizes the notion of regular expressions by allowing to apply an expression to any variable. With the new {RegEx{<variable>}} syntax, the {RegEx*} example above can be rewritten as:

Code: Select all
The syntax is thus {RegEx{<variableName>}:<expression>} and you can of course combine everything as for example in:
Code: Select all
{RegEx{FilePath[2]}:.*(.{4})}The last 4 characters of the grandparent folder are '{1}'
Note that any variable can be used in the new RegEx{} mechanism, opening the door for very sophisticated pattern extraction and capture from any of the hundred+ existing substitution variables.

This is just an example of regular expression. You can achieve the same effect with the simpler {Right{},n} function as below:
Code: Select all
The last 4 characters of the grandparent folder are '{Right{FilePath[2]},4}'
The {RegEx*} variable will continue to work exactly as before for backwards compatibility with existing templates, but this syntax is deprecated: use the new {RegEx{FilePath}} syntax in new templates as it is more readable.

One more example:
Code: Select all
{RegEx{FileHash:1}:(.{8}).*}The first 8 characters of the file content's SHA-1 hash are '{1}'
Remember that you can access a context menu listing all the available substitution variables by right-clicking in the template editors.

Again, this is just another example of regular expression. You can achieve the same effect with the simpler {Left{},n} function as below:
Code: Select all
The first 8 characters of the file content's SHA-1 hash are '{Left{FileHash:1},8}'
The string manipulation functions that have been added are summarized below:


Functions and macros are very similar and can be composed, for example consider the following:
Code: Select all
{InnerTrim{TitleCase{Replace{FileName},"_"," "}}}
What's happening here:

  • First, the underscores are replaced by spaces
  • Next, each word's first letter is capitalized
  • Finally, the inner spaces are removed
This expression transforms "an_example_of_cool_name_change" into "AnExampleOfCoolNameChange" - isn't that cool?