Regular Expressions - A Working Example

Bulk Rename Utility

Regular Expressions - A Working Example

Top  Previous  Next

Assume you have a file called Program Files, and you wish to swap the names around (e.g. Files Program). A Regular Expression which performs this task is :

 

 ^([A-Z][a-z]*) ([A-Z][a-z]*)

 

Let us break this down into components:

 

^ This means start at the beginning of the string

([A-Z][a-z]*) This is a single "group", which we will use later. What this says is that we want any capital letter, followed by any number of lower-case letters. The single capital letter is denoted by the [A-Z], i.e. allow a letter in the range A to Z. The lower-case letters are denoted by [a-z] in the same way, followed by an asterisk. This means we can allow any number of letters.

 

We then allow a single space. If I had wanted multiple spaces I would probably have typed "space asterisk", or possible ( *) to group.

 

We then have exactly the same again, i.e. we are denoting two words.

 

Notice we had two sets of brackets. Everything within each set of brackets is treated as a "grouping", and we refer to these groupings as \1, \2, \3 etc.

 

So, lets say we wanted to swap around the two words in the filename. We would put:

 

 ^([A-Z][a-z]*) ([A-Z][a-z]*)

 

For the match string, and

 

 \2 \1

 

As the replacement string. Of course, we're free to manipulate the replacements string as we like. For example, it would be quite valid to have:

 

The \2 which are used to run the \1

 

For the replacement string. This would result in:

 

The Files which are used to run the Program.

 

The above example is very precise. If we wanted to swap the first two words of a name, but keep the remaining text the same, we could put

 

 ^([A-Z][a-z]*) ([A-Z][a-z]*)(.*)

 

 \2\1\3

 

 

This says to create three groups: the first group is the first word, the second group is the second word, and the third group is everything that's left.