mac software blog software purchase frequently asked questions macintosh home page contact information mac home page

Better Software for Apple Mac osx

Using Regular Expressions

A Better Finder Rename allows you to use an advanced technique called "Regular Expression Substitution" to replace text segments.

This is an advanced technique which requires a certain familiarity with programming concepts and may thus prove inappropriate for novice computer users.

The feature is available via the "Replace using Regular Expression" action.

A regular expression defines a text (i.e. character string) pattern and assigns a variable to each component of the pattern.

The basic rules for patterns are:

  • ^   Beginning of string
  • $   End of string 
  • . Any character 
  • [   Start of character list 
  • ]   End of character list 
  • (   Start of expression group 
  • )   End of expression group 
  • |   ORs two expressions 
  • \   Escape character
  • *   Preceding expression occurs zero or more times 
  • ?   Preceding expression occurs zero or one times 
  • +   Preceding expression occurs one or more times
  • all other characters match themselves

Examples:

  • .* denotes any sequence of characters
  • .* .* denotes any string of characters that includes a space

You use "expression groups" to determine which parts of the text will be grouped together and put into a variable. You achieve this grouping by placing braces around the expression group:

  • (.*) (.*) creates two expression groups \1 and \2; \1 will contain all characters before the space and /2 will contain all characters after the space

As mentioned before the different pattern groups will be placed into substitution variables. These variables are denoted by \1, \2, \3, \4, etc.. \0 is a special variable that holds the entire matched pattern.

The substitution expression is made up from simple text characters and variables.

For instance:

  • regular expression: (.*) (.*)
  • substitution expression: \2_\1!

will yield the following name transformations:

  • hello stranger -> stranger_hello!
  • Holiday pictures -> pictures_holiday!

The [ and ] characters can enclose character lists:

  • [ab] denotes a single lowercase a or b letter
  • [a-z] any lowercase letter
  • [0-9] any digit
  • [0-9]+ any number
  • [a-z,A-Z,0-9] any letter or digit

The regular expression support of A Better Finder Select for Mac OS X (Version 7.9 and later) is based on the RegexKit framework, which itself is based on the Perl-Compatible Regular Expressions (PCRE) project.

Substitution is limited to replacing the standard substitution variables \0, \1, \2, \3, \4, \5, \6.

In essence, PCRE supports the standard regular expression syntax used by the PERL scripting language.

There are number of good tutorials and books out there:

You can get a complete and very detailed specification of what PRCE supports on the prcepattern man page.

For those who want to master this technique in earnest, we recommend the following two books: