Regular expression syntax ?

Get help with installing and running FastPictureViewer: ask questions here. Please do not post presumed bug reports or suggestions here.

Regular expression syntax ?

Postby simonfbarnes on Tue Dec 14, 2010 4:05 am

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)
simonfbarnes
 
Posts: 7
Joined: Fri Mar 12, 2010 3:53 pm

Re: Regular expression syntax ?

Postby Axel on Sun Dec 19, 2010 4:28 am

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
{RegEx:(_)?(.*?)(_)?([0-9]+)?$}MyImage#{4}-HasSuffix-{2}

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: http://msdn.microsoft.com/en-us/library/bb982727.aspx.

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 http://www.fastpictureviewer.com/help/.

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 (1.3.173.1) should correct this occasional problem.
Axel
Site Admin
 
Posts: 794
Joined: Thu Nov 06, 2008 1:54 am
Location: Geneva, Switzerland

Re: Regular expression syntax ?

Postby jlindsey on Thu Jan 19, 2012 6:37 pm

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

D{DateTaken:6}T{DateTaken:11}_{RegEx:(_)?(.*?)(_)?([0-9]+)?$}{4}
jlindsey
 
Posts: 3
Joined: Thu Jan 12, 2012 6:01 pm

Re: Regular expression syntax ?

Postby Axel on Fri Jan 20, 2012 6:10 am

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
.*(...)$

or
Code: Select all
.*(.{3})$

...so
Code: Select all
{RegEx:.*(.{3})$}{1}

...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:

http://www.regular-expressions.info/tutorial.html

Have fun!

PS: from your example I see that I could add a couple of extra "date time" formatting presets, like ISO 8601
Axel
Site Admin
 
Posts: 794
Joined: Thu Nov 06, 2008 1:54 am
Location: Geneva, Switzerland

Re: Regular expression syntax ?

Postby tstoddard on Tue May 28, 2013 11:22 pm

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?
tstoddard
 
Posts: 6
Joined: Tue May 07, 2013 2:43 pm

Re: Regular expression syntax ?

Postby Axel on Wed May 29, 2013 12:12 am

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!
Axel
Site Admin
 
Posts: 794
Joined: Thu Nov 06, 2008 1:54 am
Location: Geneva, Switzerland

Re: Regular expression syntax ?

Postby Axel on Sat Apr 09, 2016 7:05 am

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
{RegEx*:.*\\(.*)\\(.*)\\.*}{1}_{2}_{FileName}
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
{FilePath[2]}_{FilePath[1]}_{FileName}
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
{RegEx{FilePath}:.*\\(.*)\\(.*)\\.*}{1}_{2}_{FileName}
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:

{LTrim{variable}}
{RTrim{variable}}
{LRTrim{variable}}
{InnerTrim{variable}}
{AllTrim{variable}}
{SingleSpace{variable}}
{Left{variable},length}
{Right{variable},length}
{Mid{FileName},start,length}
{Upper{variable}}
{Lower{variable}}
{TitleCase{variable}}
{Reverse{variable}}
{Replace{variable},"search_regex","replacement"}

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?
Axel
Site Admin
 
Posts: 794
Joined: Thu Nov 06, 2008 1:54 am
Location: Geneva, Switzerland


Return to User-to-User



cron