A practical example of regex.

I want to transform a collections of files from this:

Into this:

I could rename each file by hand, but this would take a bit of effort, is boring and annoying. Using a regex expression and PowerRenamer I can do this in one go.

This is the regular expression I used to match the components of the file names:

(^[0-9]{16})_1_(\d\d)_(\d\d)_(\d\d\d\d)

And here is the regular expression I used to transform the file names:

$4.$2.$3 - INVOICE - CompanyName - $1

Regex is a bit cryptic, but lets break the key concepts down.

First, we need to match the 16 digit account number:

[0-9]{16}

The square brackets matches a single digit zero through 9. The curly braces tells regex to do this match sixteen times. The rest of the expression refines this a bit.

The ^ indicates to start from the beginning of the line. The parentheses puts this into a group so it can be references later by a dollar sign and a number.

The second significant part of the expression:

(\d\d)_(\d\d)_(\d\d\d\d)

Matches the month, day and year of the file name and puts them into groups so we can easily transform them putting them into a form yyyy.mm.dd.