# 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:** 11
**Page:** 4

<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: [April 11, 2019, 12:30pm UTC](https://community.mp3tag.de/t/regular-expressions/521/63 "2019-04-11T12:30:08Z")

</div>

5 posts were split to a new topic: [Extract catalogue number from album field](https://community.mp3tag.de/t/extract-catalogue-number-from-album-field/45099)

---

<div class="post-metadata">

### Author: ![ayryq](https://community.mp3tag.de/letter_avatar_proxy/v4/letter/a/4bbf92/32.png) [@ayryq](https://community.mp3tag.de/u/ayryq)
#### Post date: [June 18, 2019, 4:47pm UTC](https://community.mp3tag.de/t/regular-expressions/521/64 "2019-06-18T16:47:41Z")

</div>

Here is one to use only the last name of a composer  
`$regexp(%composer%,(.+)(?=\W),)`  
So "Johann Sebastian Bach" in the composer field could become " Bach" in the filename.

---

<div class="post-metadata">

### Author: ![LyricsLover](https://community.mp3tag.de/letter_avatar_proxy/v4/letter/l/c6cbf5/32.png) [@LyricsLover](https://community.mp3tag.de/u/LyricsLover)
#### Post date: [June 19, 2019, 6:52am UTC](https://community.mp3tag.de/t/regular-expressions/521/65 "2019-06-19T06:52:37Z")

</div>

> [@ayryq](#):
>
> Here is one to use only the last name of a composer  
> ...  
> So "Johann Sebastian Bach" in the composer field could become " Bach" in the filename.

This is only true, if the composer name is filled with "first name last name". This regex doesn't give you the "last name", it give you **the last word** filled in composer.  
So "`Bach, Johann Sebastian`" would result in `Sebastian` (last word of his first name).

---

<div class="post-metadata">

### Author: ![ayryq](https://community.mp3tag.de/letter_avatar_proxy/v4/letter/a/4bbf92/32.png) [@ayryq](https://community.mp3tag.de/u/ayryq)
#### Post date: [June 19, 2019, 12:10pm UTC](https://community.mp3tag.de/t/regular-expressions/521/66 "2019-06-19T12:10:05Z")

</div>

Yes, that's right, the last word of the composer. And as I've got it, it includes the space before that last word as well, which isn't ideal. But as my composer fields are formatted as "First Middle Last" it worked for my purpose, which was to put the composer's last name in the filename. It would fail in the case there was a suffix like "Johann Stauss Jr."

A regex to grab the first word should be even simpler and could key on the presence of a comma.

---

<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: [July 26, 2019, 12:07pm UTC](https://community.mp3tag.de/t/regular-expressions/521/67 "2019-07-26T12:07:38Z")

</div>

4 posts were split to a new topic: [Rename based on certain tag fields](https://community.mp3tag.de/t/rename-based-on-certain-tag-fields/46001)

---

<div class="post-metadata">

### Author: ![incifinci](https://community.mp3tag.de/user_avatar/community.mp3tag.de/incifinci/32/6526_2.png) [@incifinci](https://community.mp3tag.de/u/incifinci)
#### Post date: [August 2, 2019, 2:15pm UTC](https://community.mp3tag.de/t/regular-expressions/521/68 "2019-08-02T14:15:53Z")

</div>

To my (already not redactable) [earlier post](https://community.mp3tag.de/t/regular-expressions/521/27).

Be, aware, first check it out! Pay attention to multiple surnames; to paternal + maternal surnames (for example, in spanish: "paternal" y "maternal", or inversely); and languages with proper using (= family name first) of family names (for example, hungarian or japan).

---

<div class="post-metadata">

### Author: ![movtar](https://community.mp3tag.de/letter_avatar_proxy/v4/letter/m/53a042/32.png) [@movtar](https://community.mp3tag.de/u/movtar)
#### Post date: [August 17, 2019, 3:52am UTC](https://community.mp3tag.de/t/regular-expressions/521/69 "2019-08-17T03:52:45Z")

</div>

Fixing/Renaming files with invalid(invisible) characters.

I came across some MP3's with invisible characters (like Ã˜ ) in their name because of some codepage hickups. It's quite easy to fix with a simple regex search and replace.

Actions -\> Replace with regular expression  
Field: \_ALL  
regex: \xc3\x98  
Replace matches with: Ø

\xc3 = hex c3 = Ã  
\x98 = hex 98 = invisible control character (on my PC)

A full list with possible hickups can be found here:  
[UTF-8 Encoding Debugging Chart](https://www.i18nqa.com/debug/utf8-debug.html#dbg)

 ![RegExFix](https://community.mp3tag.de/uploads/default/original/2X/8/8de5dd98a15d02b9480f4acd9af4cdd2cf53a1de.png)

---

<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: [September 20, 2020, 3:48pm UTC](https://community.mp3tag.de/t/regular-expressions/521/70 "2020-09-20T15:48:07Z")

</div>

A post was split to a new topic: [Change backslash to forward slash in Genre field](https://community.mp3tag.de/t/change-backslash-to-forward-slash-in-genre-field/50247)

---

<div class="post-metadata">

### Author: ![vkostas](https://community.mp3tag.de/user_avatar/community.mp3tag.de/vkostas/32/227_2.png) [@vkostas](https://community.mp3tag.de/u/vkostas)
#### Post date: [December 12, 2020, 11:54am UTC](https://community.mp3tag.de/t/regular-expressions/521/71 "2020-12-12T11:54:34Z")

</div>

Few tips can help reduce complexity of Regular Expressions and likely make them run faster (see point 5).  
Mp3Tag uses Perl Regular Expressions.

1. **`$`** vs **`\`** _in replacement section_.  
e.g. swap artist name. Mercury, Freddy --\> Freddy Mercury  
RegExp: **`(\w+),(\w+)`**  
ReplaceWith: **`\2, \1`**  
**`\2, \1`** is equivalent to **`$2, $1`** and **`${2} ${1}`**  
The later is very useful if a fixed digit must be added after a group, e.g. **`${1}3`** adds digit 3 after group1.  
**`\w+`** captures a series of ASCII characters.

2. _Named group_.  
Ever wondering creating a complex regex and then starting counting the groups (1,2,3)? What if you create or remove a group? use **?\<...\>** to give a human readable name to your group. We create here two groups: **`first`** and **`last`**.  
Above regexp would become:  
RegExp: **`(?<last>\w+),(?<first>\w+)`**  
ReplaceWith: **`$+{first} $+{last}`**  
Note the additional **`+`** here, **`$+{first}`** which is not used in numbered groups **`$1`** , **`${1}`**.

3. _Reuse pattern_.  
Named group can be used for recurring patterns. Could make your regex more human readable.  
if we create a group **name** for our first/last name pattern **`(?\<name>[^\s]+)`** we can reuse it.  
RegExp: **`(?<last>(?<name>\w+)), (?<first>(?&name))`**  
ReplaceWith: **`$+{first} $+{last}`**  
Note that **`(?&name)`** only reuses the pattern **`name`**. Their actual captured values can be anything. The three groups **`last`** , **`name`** and **`first`**.

4. Reuse pattern relative position  
RegExp: **`(?<last>(?<name>\w+)), (?<first>(?-2))`**  
ReplaceWith: **`$+{first} $+{last}`**  
**`((?-2))`** here goes two groups behind (the 1st is its wrapper and the 2nd is the group **`name`** ).

5. _Reuse value_. **`\k{name}`**  
To capture same value use **`\k{name}`** , instead of **`(?&name)`**. This is the similar to what we normally do with **`\1`**.

6. _LookBehind_. _Magic switch_ **`\K`**.  
Perl supports only fixed length look behind (this is a pattern we want to check before the current cursor) but it since some versions includes the magic switch **`\K`**.This switch tells the system to use only the part after this switch.  
Example, delete trailing non-ASCII characters of our above pattern.  
RegExp: **`(?<first>(?\w+))\s(?<last>(?&name))\K[^\w]+`**  
ReplaceWith **`""`**  
Anything before the **`\K`** is not involved in the replacement part.

All above are 100% tested in:

- Actions (`replace with regular expression`)  
Enter source tag (e.g. artist) in **Field:**.  
Enter text **RegExp** into **Regular Expression:**.  
Enter text **ReplaceWith** into **Replaces Matches with:**.

- Function `$regexp(Text,'Find','Replace')`  
Enter source tag (e.g. %artist%) in **Text**.  
Enter text **RegExp** into **Find**.  
Enter text **ReplaceWith** into **Replace**.

- WebSources `regexpreplace "Find" "Replace"`  
Enter text **RegExp** into **Find**.  
Enter text **ReplaceWith** into **Replace**.

Happy coding & testing.

---

<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: [May 28, 2021, 9:01am UTC](https://community.mp3tag.de/t/regular-expressions/521/75 "2021-05-28T09:01:50Z")

</div>

A post was split to a new topic: [Example to remove track number from TITLE via Regexp](https://community.mp3tag.de/t/example-to-remove-track-number-from-title-via-regexp/53356)

---

<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: [May 16, 2025, 1:43pm UTC](https://community.mp3tag.de/t/regular-expressions/521/79 "2025-05-16T13:43:43Z")

</div>



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