Renaming mp3

A swapping-ground for Regular Expression syntax

Renaming mp3

Postby braciata » Mon Mar 16, 2015 8:18 pm


I would like to rename mp3s like this:

Artist & whatever - Title
Artist - Title (With whatever)

This is what I do:
(.+) \& (.+) - (.+)
\1 - \3 (With \2)

This work for simple names...

On this one it doesn't work
Dr. Dre & Eminem & Skylar Grey - I Need A Doctor
Probably because there are two &

I would like that whatever is after the 1st & goes after 'WIth'
Is that possible?

Thx in advance.
Posts: 5
Joined: Wed Mar 11, 2015 2:12 am

Re: Renaming mp3

Postby bitmonger » Thu Apr 02, 2015 1:08 am

This requires a simple fix.
Your Match in the regex was: (.+) \& (.+) - (.+).
The problem is the first (.+) is greedy and initially matches the whole filename, then backtracks until it hits the first \& and stops. To prevent this we make the + quantifier lazy by putting a ? after it.
So try the following for your match pattern:
(.+?) \& (.+) - (.+)

This will match the first \& no matter how many are in the filename.

Hope this helps,
Posts: 50
Joined: Sat Sep 22, 2007 5:05 am

Re: Renaming mp3

Postby braciata » Wed Apr 08, 2015 8:28 am

Sorry I didn't received a confimration mail of your answer.
This fix works perfectly and it helped me on another task as well. I wanted to remove the comma on artist name and it worked only when it contained just 2 words.

Michael, George & Queen > George & Queen Michael

This is what I had with more than 2 words!

Thanks a lot!

Posts: 5
Joined: Wed Mar 11, 2015 2:12 am

Return to Regular Expressions