# Regular Expressions

**URL:** https://community.mp3tag.de/t/regular-expressions/521
**Category:** Howto
**Created:** [October 16, 2003, 4:47pm UTC](https://community.mp3tag.de/t/regular-expressions/521 "2003-10-16T16:47:29Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![Florian](https://community.mp3tag.de/user_avatar/community.mp3tag.de/florian/32/5759_2.png) [@Florian](https://community.mp3tag.de/u/Florian)
#### Post date: [October 16, 2003, 4:47pm UTC](https://community.mp3tag.de/t/regular-expressions/521/1 "2003-10-16T16:47:29Z")

</div>

### 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?](https://community.mp3tag.de/t/967/1) 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.**

---

<div class="post-metadata">

### Author: ![Florian](https://community.mp3tag.de/user_avatar/community.mp3tag.de/florian/32/5759_2.png) [@Florian](https://community.mp3tag.de/u/Florian)
#### Post date: [October 16, 2003, 4:52pm UTC](https://community.mp3tag.de/t/regular-expressions/521/2 "2003-10-16T16:52:34Z")

</div>

**Trim leading/trailing spaces**

See [FAQ: Trim leading/trailing spaces](https://community.mp3tag.de/t/967/10).

---

<div class="post-metadata">

### Author: ![areve](https://community.mp3tag.de/letter_avatar_proxy/v4/letter/a/4af34b/32.png) [@areve](https://community.mp3tag.de/u/areve)
#### Post date: [October 16, 2003, 5:42pm UTC](https://community.mp3tag.de/t/regular-expressions/521/3 "2003-10-16T17:42:53Z")

</div>

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`)

---

<div class="post-metadata">

### Author: ![areve](https://community.mp3tag.de/letter_avatar_proxy/v4/letter/a/4af34b/32.png) [@areve](https://community.mp3tag.de/u/areve)
#### Post date: [December 4, 2003, 6:05pm UTC](https://community.mp3tag.de/t/regular-expressions/521/4 "2003-12-04T18:05:37Z")

</div>

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:](https://community.mp3tag.de/uploads/default/original/1X/3a79e8b0b7d169b30216785521d7c6f4f895c421.gif ":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 😛

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`**

---

<div class="post-metadata">

### Author: ![phoenixdarkdirk](https://community.mp3tag.de/letter_avatar_proxy/v4/letter/p/e274bd/32.png) [@phoenixdarkdirk](https://community.mp3tag.de/u/phoenixdarkdirk)
#### Post date: [April 11, 2004, 5:50pm UTC](https://community.mp3tag.de/t/regular-expressions/521/5 "2004-04-11T17:50:59Z")

</div>

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

---

<div class="post-metadata">

### Author: ![areve](https://community.mp3tag.de/letter_avatar_proxy/v4/letter/a/4af34b/32.png) [@areve](https://community.mp3tag.de/u/areve)
#### Post date: [May 30, 2004, 6:20pm UTC](https://community.mp3tag.de/t/regular-expressions/521/6 "2004-05-30T18:20:57Z")

</div>

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

---

<div class="post-metadata">

### Author: ![mll](https://community.mp3tag.de/letter_avatar_proxy/v4/letter/m/6a8cbe/32.png) [@mll](https://community.mp3tag.de/u/mll)
#### Post date: [July 14, 2004, 7:27am UTC](https://community.mp3tag.de/t/regular-expressions/521/7 "2004-07-14T07:27:08Z")

</div>

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 🙂 :

Cheers,

MLL

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

---

<div class="post-metadata">

### Author: ![ThurstonX](https://community.mp3tag.de/letter_avatar_proxy/v4/letter/t/ecae2f/32.png) [@ThurstonX](https://community.mp3tag.de/u/ThurstonX)
#### Post date: [July 31, 2004, 8:34pm UTC](https://community.mp3tag.de/t/regular-expressions/521/8 "2004-07-31T20:34:53Z")

</div>

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

---

<div class="post-metadata">

### Author: ![RevRagnarok](https://community.mp3tag.de/letter_avatar_proxy/v4/letter/r/b487fb/32.png) [@RevRagnarok](https://community.mp3tag.de/u/RevRagnarok)
#### Post date: [November 18, 2004, 10:23am UTC](https://community.mp3tag.de/t/regular-expressions/521/9 "2004-11-18T10:23:34Z")

</div>

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](http://www.revragnarok.com/blog/blog/2004/04/10/21.34) 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](http://isbn.nu/0596002890) 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. 😉 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._

---

<div class="post-metadata">

### Author: ![Florian](https://community.mp3tag.de/user_avatar/community.mp3tag.de/florian/32/5759_2.png) [@Florian](https://community.mp3tag.de/u/Florian)
#### Post date: [November 18, 2004, 12:34pm UTC](https://community.mp3tag.de/t/regular-expressions/521/10 "2004-11-18T12:34:05Z")

</div>

RevRagnarok,

> [@](#):
>
> 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.

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

Best regards,  
~ Florian

---

<div class="post-metadata">

### Author: ![nickless](https://community.mp3tag.de/user_avatar/community.mp3tag.de/nickless/32/36_2.png) [@nickless](https://community.mp3tag.de/u/nickless)
#### Post date: [March 29, 2005, 7:22pm UTC](https://community.mp3tag.de/t/regular-expressions/521/11 "2005-03-29T19:22:19Z")

</div>

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

---

<div class="post-metadata">

### Author: ![ThurstonX](https://community.mp3tag.de/letter_avatar_proxy/v4/letter/t/ecae2f/32.png) [@ThurstonX](https://community.mp3tag.de/u/ThurstonX)
#### Post date: [October 14, 2005, 11:01pm UTC](https://community.mp3tag.de/t/regular-expressions/521/12 "2005-10-14T23:01:21Z")

</div>

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

---

<div class="post-metadata">

### Author: ![dano](https://community.mp3tag.de/user_avatar/community.mp3tag.de/dano/32/6_2.png) [@dano](https://community.mp3tag.de/u/dano)
#### Post date: [December 28, 2005, 2:12pm UTC](https://community.mp3tag.de/t/regular-expressions/521/13 "2005-12-28T14:12:25Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Snykch](https://community.mp3tag.de/letter_avatar_proxy/v4/letter/s/8baadc/32.png) [@Snykch](https://community.mp3tag.de/u/Snykch)
#### Post date: [February 19, 2006, 1:35pm UTC](https://community.mp3tag.de/t/regular-expressions/521/14 "2006-02-19T13:35:47Z")

</div>

**Convert artist names in the form 'Artist, The'**

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

---

<div class="post-metadata">

### Author: ![Michaelm](https://community.mp3tag.de/letter_avatar_proxy/v4/letter/m/3be4f8/32.png) [@Michaelm](https://community.mp3tag.de/u/Michaelm)
#### Post date: [April 22, 2006, 10:56am UTC](https://community.mp3tag.de/t/regular-expressions/521/15 "2006-04-22T10:56:43Z")

</div>

**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"

---

<div class="post-metadata">

### Author: ![Blues\_Fan](https://community.mp3tag.de/letter_avatar_proxy/v4/letter/b/7ab992/32.png) [@Blues\_Fan](https://community.mp3tag.de/u/Blues_Fan)
#### Post date: [March 25, 2007, 5:05pm UTC](https://community.mp3tag.de/t/regular-expressions/521/16 "2007-03-25T17:05:34Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![neilpa](https://community.mp3tag.de/letter_avatar_proxy/v4/letter/n/41988e/32.png) [@neilpa](https://community.mp3tag.de/u/neilpa)
#### Post date: [November 20, 2007, 5:16am UTC](https://community.mp3tag.de/t/regular-expressions/521/17 "2007-11-20T05:16:35Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![nmiller\_co](https://community.mp3tag.de/letter_avatar_proxy/v4/letter/n/6a8cbe/32.png) [@nmiller\_co](https://community.mp3tag.de/u/nmiller_co)
#### Post date: [January 21, 2008, 11:13pm UTC](https://community.mp3tag.de/t/regular-expressions/521/18 "2008-01-21T23:13:25Z")

</div>

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](https://community.mp3tag.de/t/6100/1). 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...

---

<div class="post-metadata">

### Author: ![Harakiri](https://community.mp3tag.de/user_avatar/community.mp3tag.de/harakiri/32/142_2.png) [@Harakiri](https://community.mp3tag.de/u/Harakiri)
#### Post date: [September 1, 2008, 8:02pm UTC](https://community.mp3tag.de/t/regular-expressions/521/19 "2008-09-01T20:02:52Z")

</div>

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](https://community.mp3tag.de/t/5635/1)

---

<div class="post-metadata">

### Author: ![KCE](https://community.mp3tag.de/letter_avatar_proxy/v4/letter/k/c57346/32.png) [@KCE](https://community.mp3tag.de/u/KCE)
#### Post date: [October 24, 2008, 8:08pm UTC](https://community.mp3tag.de/t/regular-expressions/521/20 "2008-10-24T20:08:20Z")

</div>

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:**

[Next page](https://community.mp3tag.de/t/regular-expressions/521.md?page=2)
