Add a leading zero in the middle of a title or album


I've been wrestling with this for several hours now, to no avail. I have tagged all my classical music so that the track title contains the name of the composer, work and movement and the album title contains the composer, work and album artists. This was all done long ago before most control point software could properly handle classical music tags.

The issue I have now is that my control point software does not properly handle numerical order in these tags, i.e. I get things showing up as 1, 10, 11, ...2, 20, 21, etc.

To get the ordering I want I wish to add a leading zero to the number of the work, which is in the middle of the string. For example, Mahler Symphony No. 9 becomes Mahler Symphony No. 09. But I need to add the zero to only the first instance of a number, because the movement number or opus number is also in the string.

In other words, I want this:
(random text) number (more random text) another number

to become this:
(random text) 0number (more random text) another number

In the regular expression topic I found something for this in the track tag, ^(\d)$ - 0$1
But I've tried every permutation of this expression I could think of and nothing is working correctly. Any help would be greatly appreciated.

Is the "random text" really in parenthesis?

I'll try without first:
Apply an action of the type "replace with regular expression" for the field.
Search string: (.) (\d+) (.)
Replace string: $1 0$2 $3

First of all, thanks for the reply. Second, no, the random text is not in parentheses.

I tried your solution. We're on the right track, but it's still not quite right. Here's an example:

original tag:
Mozart Piano Concerto No. 1 - 1 - Allegro

running it through your action, it becomes:
Mozart Piano Concerto No. 1 - 01 - Allegro

I'm a complete novice with regular expressions, but I tried several variations I found looking around the web, (\d+), (\d?), (\d+?), (\d?+). They all added the leading zero to the second 1 instead of the first.

Any other ideas?

Yeah, this is the problem with examples that do not match the original.
Try this as search string:
(.?) (\d+) (.)
The replace string stays the same.

That did the trick, thanks so much.

I see now that I should have given you an explicit example up front. I just finished reading an online tutorial for regular expression quantifiers. Between lazy, greedy, docile and possessive, I see there is a lot of nuance in the topic that is not immediately obvious. I might have figured it out eventually on my own, but I very much appreciate that there are people like you willing to help out us novices.

Well, this is interesting. I've been applying your Action to my collection. It works here:

Beethoven Piano Trio No. 1 - Beaux Arts Trio
Beethoven Piano Trio No. 2 - Beaux Arts Trio

but not here:

Beethoven Piano Trio No. 4, "Gassenhauer" - Beaux Arts Trio
Beethoven Piano Trio No. 5, "Ghost" - Beaux Arts Trio

The only difference appears to be the comma immediately following the digit. Perhaps it's beyond the scope of this forum, but can you offer an explanation for why the comma "breaks" the expression? Thanks.

... because the search pattern
(.?) (\d+) (.)
looks for any combination of characters, a blank, a digit of any length, a blank, any combination of characters - but no comma following immediately a digit.
You could try
(.?) (\d+)(.),$1 0$2$3)
and see if this is more universal.