Regular Expressions

This topic collects some useful regular expressions.

If you created a regular expression which solves a common task, please post it here and give a short description what it is supposed to do.

These regular expressions can be used with the action type Replace with regular expression. Please look at FAQ: How do I create a new action? to learn more about actions in Mp3tag.

Important: The regular expressions are user-contributed solutions of individual tasks. Test with a copy of your files first before applying them. Some of the expressions also apply to _ALL or _TAG, which might be too coarse-grained for you, if you want to only change a specific tag field.

If you have questions or problems with a regular expression, please open a separate topic.

4 Likes

Trim leading/trailing spaces

See FAQ: Trim leading/trailing spaces.

1 Like

Regular expression: ^\s*[0-9]+\s*-\s*
Replace with:

This one will remove the track-number (if followed by a dash) and white space at the beginning of a string
(for instance 01 - Come Together will become Come Together)

Regular expression: ^\s*[0-9]+\s+
Replace with:

This one will remove the track-number if followed by a white space
(for instance 01 Come Together will become Come Together)

1 Like

Here is another one.
I don't think it will be useful as such to a lot of people, but with REGEXP, examples are never too many :rolleyes:

My files were tagged as such:
name of the artist.2003AL-name of the album
(the 2003AL meaning: released in 2003, Album, Live, but it could be 1977A, or 1995IR)
Weird, I know :stuck_out_tongue:

I wanted to change that to a simpler
2003-name of the album

Here is what I did:
Regular expression: .*\.([0-9]*).*-(.+)
Replace with: $1-$2

This regular expression fixes tracknumbers from iTunes in the [track]/[numtracks] format (like 3/12).

Regex:

^(\d+)/\d+

Replace with:

$1

This can be applied to every file and it will only correct ones that have the / in them. Good luck!

According to RevRagnarok's suggestions below, I've made the expression less greedy.

Another one, that switches first and last names (for instance, Jacques Brel will become Brel, Jacques):
Regular Expression: ^(.+)\s(.+)$
Replace with: $2, $1

1 Like

Used to use this basic regexp :

Applied on TRACK, find ^(\d)$ and replace by 0$1

It adds a leading 0 to the track number. I was thinking about enhancing it to apply it only to 1-digit numbers... when I discovered Ctrl+K :slight_smile: :

Cheers,

MLL

According to RevRagnarok's suggestions below, I've made the expression less greedy

If you have a filename:

  • 01 Trackname
and want it to be
  • 01 - Trackname
create an Action with the following properties:
  • Field: _FILENAME
  • Regular Expression: ([0-9]+)\s
  • Replace matches with: $1 - $2

Edit: Topic merged by moderator.
1 Like

I've noticed a lot of these RegExs 'go overboard' in their matching. A key to good expressions are limiting (1) the false matches and (2) the how long the engine needs to analyze the string.

For example, my two favorite expressions I have posted here are both above on this page. However, they have very greedy matchers that can easily result in lost data.

phoenixdarkdirk's - why NOT limit the track 'numbers' to digits with \d+ ?
mll's - really went overboard when a simple ^(\d)$ would've done it.

I highly recommend this book if you are serious about using REs. Of course, I recommend trying to find it at a technical library, because if you look it over a little you may realize you were just kidding and save yourself the money.

Of course, when we are using them on a handful of MP3 files, it's no big deal. :wink: It's when you are handling megabytes of text files that it really matters.

Edit 5 Apr 2013 - After over eight years, fixed the URL to my blog.

RevRagnarok,

Thanks a lot for your suggestions. I've changed the expressions you've mentioned to be less greedy.

Best regards,
~ Florian

1 Like

This RegEx will convert abbreviations composed of single chars and points between (and/or behind it) to uppercase.
Single chars without points around remains lowercase, except if a "-"char and a space is before it. (Example: "Songname - A text")

RegEx: ( |\.|^)(\w)(?= |\.|$)
Replace with: $1$upper($2)and
RegEx: ([^-])( \u )
Replace with: $1$lower($2)

Example: a.b a Reg.eX in p.o.d p. diidi e.t. - a bad a.i

==>
A.B a Reg.eX in P.O.D P. diidi E.T. - A bad A.I

Both regular expressions should be executed one after another

Use

RegEx: ( \u )
Replace with: $lower($1)instead of second RegEx to have chars after " - " lowercase too. (Example: "Songname - a text")

Regards
nickless

Edit: removed some unnecessary characters from RegEx

Title Tag Conversion

This 2-step action does:

  1. set Title tag to Filename
  2. convert titles using, e.g.:
    01 - Track Name
    to
  3. Track Name

First step:
Format Value
Field: TITLE
Formatstring: %_FILENAME%

Second step:
Set a new Replace with Regular Expression action
Field: TITLE
Regular expression:
^([0-9]+)\s*-\s*
Replace matches with:
$1. $2

I use the first format for all file names, but my iRiver displays the title tag, which looks better (and saves one character) using the latter. For me it's a 2-step process: Filename-to-Tag (%TITLE% only) and then convert Title tag. Assumes your filename is the way you want it, of course.

Upper case for Roman numbers

Regular expression:
\b(?:M{0,3})(?:D?C{0,3}|C[DM])(?:L?X{0,3}|X[LC])(?:V?I{0,3}|I[VX])(?=(\.\s|\s|\)|$))

Replace matches with:
$upper($0)
[ ] case-sensitive comparison

Convert artist names in the form 'Artist, The'

Regular expression: (.*),\sthe$
Replace with: The $1

1 Like

And the other way around

Move "The " to the end

Regular expression: ^The (.+)
Replace with: $1, The

So "The Cure" becomes "Cure, The"
And "The a whole lot of words" becomes "a whole lot of words, The"

It's taken me far too long to figure it out but I've finally got it working.

To change Horne, Lena to Lena Horne or
Hawkins, Coleman With Manny Albam & His Orchestra to Coleman Hawkins With Manny Albam & His Orchestra
Field: ARTIST
Regular Expression: ^([\w]+),\s([\w]+)

Replace Matches with: $2 $1

I hope I didn't miss the answer in here somewhere. If there's an easier or better way please feel free to help.

1 Like

This regex can remove/replace all but the last dot from a string. It's useful for people like me who prefer to limit filename chars to a-z, 0-9, '_', and '-'. We need to save that last dot for file extensions.

Regex: .

Assumes file extensions are 2-4 chars long (not including the dot). If you are curious how this works google regex lookaround.

I recently converted all my wma files to mp3 only to realize that all the tags were lost. My music is in the form:

...\artist\album\track title.mp3

MP3TAG came to my rescue! Not sure whether there was an easier way to do this, but I got started by reading this. Sorry if these are lame ways of doing it, but it's my 1st experience with regexp.

format artist:
$regexp(%_folderpath%,.+\\(.+)\\(.+)\\,$1)

format album:
$regexp(%_folderpath%,.+\\(.+)\\(.+)\\,$2)

Next 2 I got from ThurstonX in this post.

format track:
$regexp(%_filename%,(\d*)\s?(.+),$1)

format title:
$regexp(%_filename%,(\d*)\s?(.+),$2)

This program saved me TONS of time...

Another member helped me creating one of the most useful REGEXP that i use so far:

From this:

Röyksopp - Beautiful DAY withOUT yoU (Rex The Dog Remix)

To this:

Röyksopp - Beautiful day without you (Rex The Dog Remix)

USING:

Action type: Replace with regular expression
Field: TITLE
Regular expression: ^(.*?)((|$)
Replace matches with: $caps3($1)$2
[ ] case-sensitive comparison

See the original thread here

Really simple but this will remove any parenthesis and its contents in any part of the string

Example:
Blah (blah1) (blah 2)
Result:
Blah

Regular expression: (.+?)

Replace matches with: