OK, here's how it works. Reiterating the expression here:
if(Regex([Filename (name)],
/#
^([^-]+) - (.+?)\s+(\d+)-(\d+)-(\d+)( \(Hour.+\))?#/),
[R1] - [R5][R4][R3] [R2][R6],
Filename(,0))
we'll break it into components. As described in the
Expressions wiki page, Regex takes two mandatory arguments, with a third optional argument that defaults to the value of 0. We're using that mode here - it is a test-only mode, with no output (only captures).
The goal is to match:
Artist -
Descriptor dd-
mm-
yyyy (Hour #)where the trailing portion "
(Hour #)" is optional. For now, we'll assume Artist does not contain a dash character.
The regular expression itself, color-coded here to match the above components is:
^([^-]+) -
(.+?)\s+
(\d+)-
(\d+)-
(\d+)( \(Hour.+\))?These can be interpreted as:
RE | Description |
^([^-]+) | match any non-dash character one or more times, and remember that (parens - [R1]) |
" - " | match a literal space dash space (quotes used here to see the spaces) |
(.+?) | match one or more characters (the .+), as few as possible (the ?), and remember that (parens - [R2]) |
\s+ | match one or more whitespace characters |
(\d+) | match one or more digits, and remember that (parens - [R3]) |
- | match a dash |
(\d+) | match one or more digits, and remember that (parens - [R4]) |
- | match a dash |
(\d+) | match one or more digits, and remember that (parens - [R5]) |
( \(Hour.+\))? | optionally, match the group of an opening parenthesis (quoted), followed by a space, followed by Hour, followed by one or more characters, followed by a closing parenthesis (quoted), and remember that (parens - [R6]) |
The parenthesis around the expressions store what was matched into memories. These, in MC are named as [R1], [R2], ... [R9]. We're using 1 through 6 here.
In mode 0 of Regex(), we test only that the expression matches. If it does, with the help if the If() statement, we'll output the captured [Rn] values in the order that expresses the desired rearrangement. In this case, it is:
[R1] - [R5][R4][R3] [R2][R6]
For the case when the regular expression pattern does not match the input file name, we'll just return the filename itself, minus the extension (so the rename tool will make no changes to this file). We'll employ the Filename() function with mode 0:
Filename(,0)
Now, only files that match the pattern will be renamed; others will remain unchanged.
The /# and #/ inside Regex() simply tells MC to not try to interpret the contents, since it conflicts with MCs own language - it is a form of quoting. These don't have to be used, but without them, you must take care of MC-quoting anything where there is conflict between MC's language and REs. Just use them.