# Help building correct Regular Exprssions - https://regex101.com/

**URL:** https://community.mp3tag.de/t/help-building-correct-regular-exprssions-https-regex101-com/62470
**Category:** Support
**Created:** [October 19, 2023, 9:42pm UTC](https://community.mp3tag.de/t/help-building-correct-regular-exprssions-https-regex101-com/62470 "2023-10-19T21:42:01Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![gstalnaker](https://community.mp3tag.de/letter_avatar_proxy/v4/letter/g/aca169/32.png) [@gstalnaker](https://community.mp3tag.de/u/gstalnaker)
#### Post date: [October 19, 2023, 9:42pm UTC](https://community.mp3tag.de/t/help-building-correct-regular-exprssions-https-regex101-com/62470/1 "2023-10-19T21:42:01Z")

</div>

Hoping I'm not doing anything amiss by recommending this really terrific website one can use to build regular expressions and test them against a known string:

> **[regex101: build, test, and debug regex](https://regex101.com/)**
>
> Regular expression tester with syntax highlighting, explanation, cheat sheet for PHP/PCRE, Python, GO, JavaScript, Java, C#/.NET, Rust.

I just today was able to use it to figure out how to use the folder for some mp3 files to put a value in the Discnumber tag (folders are based on Vol#CD# of the Bach Complete Edition from Teldec/Warner Music, and each contains the disc number X of the total, 153). I copied one of the folder names into the website field, then built the RegEx with capture groups to get to just the Disc number.

Folder: `Bach 2000 v03CD15 045 - Cantatas BWV 147-149`  
RegEx: `(^\w+\s\w+\s\w+\s)(\d{3})(.*)`

Match 1: 0-45; Bach 2000 v03CD15 045 - Cantatas BWV 147-149/  
Match 2: 0-18; Bach 2000 v03CD15  
Match 3: 18-21; 045  
Match 4: 21-45; - Cantatas BWV 147-149/

Which got me to, in mp3tag:

Format: Discnumber: `$regexp(%_directory%,^(\w+\s\w+\s\w+\s)(\d{3})(.*),$2)`

This really is magic! With 153 folders, that would have been a lot of hand editing.

The site has an explanation of the RegEx parsing, both good and bad, and if grouping is done, what is contained in each grouping, and an on-screen reference for all of the various RegEx.

---

<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: [October 19, 2023, 10:16pm UTC](https://community.mp3tag.de/t/help-building-correct-regular-exprssions-https-regex101-com/62470/2 "2023-10-19T22:16:35Z")

</div>

Have you used the [forum search](https://community.mp3tag.de/search?q=regex101) before and searched for regex101?  
I just found 45 results mentioning this site. 😇

But yes, you are right, it is a great site to test new and let explain existing regular expressions.

Just in case you are looking for a tool to visualize (and explain) your regular expressions:

> [@Tool to visualize (and explain) regular expressions?](https://community.mp3tag.de/t/tool-to-visualize-and-explain-regular-expressions/62328):
>
> Out of curiosity, did you make that visualization yourself or are you using a tool I'm not aware of? Looks really neat to quickly understand a more complicated regex.

---

<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 20, 2023, 1:14pm UTC](https://community.mp3tag.de/t/help-building-correct-regular-exprssions-https-regex101-com/62470/3 "2023-10-20T13:14:02Z")

</div>

Thanks for sharing! Despite that there are some mentions of the site here in the community, I find the description of your process for arriving at a solution a valuable contribution.

Thanks for sharing your excitement! 🙌

---

<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: [October 20, 2023, 1:53pm UTC](https://community.mp3tag.de/t/help-building-correct-regular-exprssions-https-regex101-com/62470/4 "2023-10-20T13:53:42Z")

</div>

> [@gstalnaker](#):
>
> Format: Discnumber: `$regexp(%_directory%,^(\w+\s\w+\s\w+\s)(\d{3})(.*),$2)`

Please be aware that this regular expression only works if your fourth value `(\d{3})` always has exactly 3 numbers. As you can see in [this regex101-examples, some possible variants would not match.](https://regex101.com/r/bmEOBL/1)

IMHO if you really only need the DISCNUMBER from the directory name It would be easier to use the Convert Filename -\> Tag and this formatstring:  
`%dummy% v03CD%DISCNUMBER% %dummy%\%dummy%`

 ![image](https://community.mp3tag.de/uploads/default/original/2X/d/d9ccf6f3f417cf1d6ddb7f3aaf3b5b8bcff80735.png)  
The Convert function has also the advantage that you can immediately see the result as  
**DISCNUMBER** 15  
For more details about the Convert Filename -\> Tag please see the [F.A.Q:](https://community.mp3tag.de/t/converters-to-rename-files-or-to-import-tags/8016/3#how-to-import-parts-of-the-folder-structure-into-the-tag-2).

This does not at all mean that your contribution is not valuable. It is just important to know that regular expressions only works for exactly the situation they were built for.

---

<div class="post-metadata">

### Author: ![gstalnaker](https://community.mp3tag.de/letter_avatar_proxy/v4/letter/g/aca169/32.png) [@gstalnaker](https://community.mp3tag.de/u/gstalnaker)
#### Post date: [October 20, 2023, 3:56pm UTC](https://community.mp3tag.de/t/help-building-correct-regular-exprssions-https-regex101-com/62470/5 "2023-10-20T15:56:59Z")

</div>

Thanks! That was not on my radar as an option. But it will be in future.

---

<div class="post-metadata">

### Author: ![gstalnaker](https://community.mp3tag.de/letter_avatar_proxy/v4/letter/g/aca169/32.png) [@gstalnaker](https://community.mp3tag.de/u/gstalnaker)
#### Post date: [October 20, 2023, 4:02pm UTC](https://community.mp3tag.de/t/help-building-correct-regular-exprssions-https-regex101-com/62470/6 "2023-10-20T16:02:24Z")

</div>

P.S. I am aware thet (\d{3}) gets exactly 3 digits as the "disc# of total" value was a 3-digit number. I had issues trying to use (\d+) which was too greedy. That said, your reference to Convert Filename seems to me a much, much better solution as is does not involve RegEx which I find (even though I am a retired 30-year IT guy) so arcane as to be the equivalent of Minoan Linear A. I've never before needed to use a part of a folder/filename for tagging and had never expored this option. Now I know. So, thanks again.

---

<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: [October 20, 2023, 4:07pm UTC](https://community.mp3tag.de/t/help-building-correct-regular-exprssions-https-regex101-com/62470/7 "2023-10-20T16:07:38Z")

</div>

> [@gstalnaker](#):
>
> Now I know. So, thanks again.

You're welcome.

Feel free to let us know if you need some specific regular expression or another way to adjust collections like yours with 153 CDs.

---

<div class="post-metadata">

### Author: ![system](https://community.mp3tag.de/uploads/default/original/2X/c/ce7035d426cb755a7916793326d23b465222a407.png) [@system](https://community.mp3tag.de/u/system)
#### Post date: [February 9, 2026, 12:30pm UTC](https://community.mp3tag.de/t/help-building-correct-regular-exprssions-https-regex101-com/62470/8 "2026-02-09T12:30:10Z")

</div>


