Regular expressions are a more powerful (and therefore complicated) form of wildcard pattern matching. Like standard pattern matching, they can be used throughout Opus. Generally, you have to specifically enable the use of regular expressions in a given situation - by default, Opus will assume standard pattern matching. For example, the Advanced Rename dialog has a regular expression mode that you must select before regular expressions can be used.
One advantage regular expressions have over standard pattern matching is they can enable a form of search and replace in certain functions. As an example, this is used in the Rename command. The "search" string is specified as a pattern to match against the original names of files. That pattern can indicate capture groups - expressions in the source string that are captured, and can be carried over to the new string (which acts as the "replace" string). As an example, imagine the Rename dialog is set to regular expression mode, with the following patterns supplied:
Old Name: The (.*) Backup\.(.*)
New Name: \1.\2
The two (.*) tokens in the old name string are capture groups - they "capture" whatever is matched by the expression within the parentheses. In this case, the expression inside the brackets is .* which simply means "match anything". So what this pattern will do is match any filename beginning with The and ending in Backup, and it will capture the middle of the filename for later use. The second (.*) will capture the file extension. The new name string can then re-use the captured text, and this is indicated with the \1 and \2 markers. So as an example, the original filename The Lord Of The Rings Backup.avi would be renamed to Lord Of The Rings.avi. \1 refers to the first capture group, \2 to the second, and so on.
If you need the new name string to contain a literal \, use two together. For example, abc\\xyz will turn into abc\xyz.
When used with the Rename command only, the old name pattern can be followed with a # character to indicate that the search and replace operation should be repeated multiple times. For example, the following regular expression rename will remove all spaces from the filename:
Old Name: (.*)\s(.*)#
New Name: \1\2
The # causes the search and replace to be repeated until the new name no longer changes. You can also specify a maximum repetition count by appending a number, for example #5 at the end would repeat the operation no more than five times.
There are many different variants of regular expression; by default Opus uses what's called TR1 ECMAScript. Microsoft has a page on TR1 that goes into far more detail than this help file can.
Token | Description | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
^ |
Start of a string. For example: |
||||||||||||||||
$ |
End of a string. For example: |
||||||||||||||||
. |
Any single character. For example: |
||||||||||||||||
* |
0 or more of previous expression. For example: |
||||||||||||||||
+ |
1 or more of previous expression. For example: |
||||||||||||||||
? |
0 or 1 of previous expression. For example: |
||||||||||||||||
| |
Alternation (logical or). For example: |
||||||||||||||||
{} |
Quantifier. For example: |
||||||||||||||||
[] |
Character set. For example: |
||||||||||||||||
[^] |
Negative character set. For example: |
||||||||||||||||
() |
Expression / capture group. For example: |
||||||||||||||||
\ |
Escape character.
It is also used to mark several character classes, which are shorthand ways to specify various common [] character sets (see below). For example: |
||||||||||||||||
\w |
Word character. For example: |
||||||||||||||||
\W |
Non-word character. |
||||||||||||||||
\s |
Space character. |
||||||||||||||||
\S |
Non-space character. |
||||||||||||||||
\d |
Digit character. |
||||||||||||||||
\D |
Non-digit character. For example: |