Regular Expressions


#1

This thread 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.

-> If you have problems with a regular expression, please open a separate topic.

Publishing useful example regular expression actions
Hilfe zu Regulären Ausdrücken
How to add a leading zero for tracknumbers via regexp
Mp3tag Linksammlung
Regular Expression für Römische Ziffern
Move ft. artist to title
How to adjust this Regexp for capitalization?
[X] Regular Expressions (Regex) - unintended repetition of strings
Regexp not working as expected in convert --> tag - filename ?
Some "comma" Help Please
Regular Expressions question
Moving "The" to the end
Dateiname - Dateiname: Punkte entfernen
Filename formatieren ???
Smart search
anyway to retrieve info from a folder name and input it into tag field??
RegEx Help
Rare substitution in filename
Simple Action Question
Remix in eckige Klammer setzen
Filter - Platzhalter verwenden
action does not work
WEB Link zerlegen bzw. trimmen
Exportdatei HTML und Aktionen
Two Regex Questions
Action to create ARTISTSORT tag
Hilfe bei Tag-Automatisierung
Delete Ending Space
#2

Trim leading/trailing spaces

See FAQ: Trim leading/trailing spaces.


Why doesn't this remove leading spaces?
Leerzeichen einfügen?
#3

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)


Remove track # from Title field
Track numbers taken away from Title field?
removing track numbers
#4

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


#5

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.

Removing a slash?
#6

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


How to rearrange artist name
Automatiucally switch [first name] [last name] by script?
Nachname, Vorname drehen
Artist ( Interpret ) Namen Tauschen z.B. Cocker, Joe nach Joe Cocker
#7

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


#8

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.

Wie Zahlen in MP3-Name 101 in 1.01 ändern?
#9

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.


#10

RevRagnarok,

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

Best regards,
~ Florian


#11

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


Making specific words lowercase?
#12

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.


#13

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

Can anyone help me create an action to correct word case?
Römische Zahlen korrigieren
Action group to captitalize Roman numerals
Regular Expression für Römische Ziffern
#14

Convert artist names in the form 'Artist, The'

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


Changing "The Beatles" to "Beatles, The"
how to swap two words in Artists field?
Sort Composer Column Sorting
"The" in Ordnerstruktur und Tag unterschiedlich
Falsche Tags automatisch korregieren
#15

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"


Remove "the"
"The Title" to "Title, The"
"The" in Ordnerstruktur und Tag unterschiedlich
Convert all "The Artist" to "Artist, The"
Rename folder
"The" bei Interpreten HINTEN anhängen
Automating Changes to Artist Values
Words Count
#16

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.


Nachname, Vorname drehen
String to Reverse Sort Order Tag
#17

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.


#18

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...


#19

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


#20

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: