# File rename with regexp and tag contents, need help

**URL:** https://community.mp3tag.de/t/file-rename-with-regexp-and-tag-contents-need-help/11920
**Category:** Support
**Created:** [April 26, 2011, 1:54pm UTC](https://community.mp3tag.de/t/file-rename-with-regexp-and-tag-contents-need-help/11920 "2011-04-26T13:54:08Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![anon40688970](https://community.mp3tag.de/letter_avatar_proxy/v4/letter/a/898d66/32.png) [@anon40688970](https://community.mp3tag.de/u/anon40688970)
#### Post date: [April 26, 2011, 1:54pm UTC](https://community.mp3tag.de/t/file-rename-with-regexp-and-tag-contents-need-help/11920/1 "2011-04-26T13:54:08Z")

</div>

I have filenames that follow these patterns:

[genre] title.mp3  
[genre] title (x of y).mp3  
[genre] title (Episode n).mp3

I'm looking for an action that renames the files to:  
[genre] title {%publisher%}.mp3  
[genre] title {%publisher%}(x of y).mp3  
[genre] title {%publisher%}(Episode n).mp3

%publisher% is present in the id3-tag.

With a real-world regexp engine I could use

(.+?)\s\*((Episode \d+)|(\d+ of \d+)|)(.mp3)

as matching expression and

$1 {%publisher%}$2$3

for the replacement part. In mp3tag i tried to use a Format Value action with

Field: \_FILENAME  
Format String: $regexp(%\_filename%,(.+?)\s\*((Episode \d+)|(\d+ of \d+)|)(.mp3),$1 {%publisher%}$2$3)

as well as some other variations, but nothing works (ie matches). Anybody able to help me translating standard regexp into the mp3tag flavor here? I guess i am missing some undocumented things or need to escape parts of the format string.

---

<div class="post-metadata">

### Author: ![DetlevD](https://community.mp3tag.de/user_avatar/community.mp3tag.de/detlevd/32/123_2.png) [@DetlevD](https://community.mp3tag.de/u/DetlevD)
#### Post date: [April 26, 2011, 2:38pm UTC](https://community.mp3tag.de/t/file-rename-with-regexp-and-tag-contents-need-help/11920/2 "2011-04-26T14:38:22Z")

</div>

> [@anon40688970](#):
>
> ... I guess i am missing some undocumented things ...

I am not aware of using a tag-field content in the replace section of the $regexp function.

DD.20110426.1840.CEST

---

<div class="post-metadata">

### Author: ![DetlevD](https://community.mp3tag.de/user_avatar/community.mp3tag.de/detlevd/32/123_2.png) [@DetlevD](https://community.mp3tag.de/u/DetlevD)
#### Post date: [April 26, 2011, 2:47pm UTC](https://community.mp3tag.de/t/file-rename-with-regexp-and-tag-contents-need-help/11920/3 "2011-04-26T14:47:26Z")

</div>

You can start to play with this expression ...

**$regexp('{'%PUBLISHER%'}==='%\_filename%,'^(.+?)===(\[.+?\]\s\*.+?)$','$2 $1')**

From:  
"T:\TEST[genre] title.mp3"  
To:  
"T:\TEST[genre] title {publisher}.mp3"

DD.20110426.1852.CEST

---

<div class="post-metadata">

### Author: ![anon40688970](https://community.mp3tag.de/letter_avatar_proxy/v4/letter/a/898d66/32.png) [@anon40688970](https://community.mp3tag.de/u/anon40688970)
#### Post date: [April 26, 2011, 3:09pm UTC](https://community.mp3tag.de/t/file-rename-with-regexp-and-tag-contents-need-help/11920/4 "2011-04-26T15:09:49Z")

</div>

Ah, interesting way to get the %publisher% into the regexp. I was about to try a similar approach with Guess Values after you've mentioned that substitution does not work in the replacement section.

This one works, using your approach, which i like very much.

Format Value:  
Field: %\_FILENAME%  
Format String: $regexp('{'%publisher%'}'=====%\_FILENAME\_EXT%,^(.+?)=====(.+?)\s\*((Episode \d+)|(\d+ of \d+)|)(.mp3),$2 $1$3$4)

Thank you very much.

---

<div class="post-metadata">

### Author: ![pone](https://community.mp3tag.de/user_avatar/community.mp3tag.de/pone/32/272_2.png) [@pone](https://community.mp3tag.de/u/pone)
#### Post date: [April 26, 2011, 3:21pm UTC](https://community.mp3tag.de/t/file-rename-with-regexp-and-tag-contents-need-help/11920/5 "2011-04-26T15:21:21Z")

</div>

Why not:  
**$regexp(%\_filename%,%title%,%title% {%publisher%})**

or to avoid mistakes with title names which are identical to the genre or episode:  
**$regexp(%\_filename%,%genre%']' %title%,%genre%']' %title% {%publisher%})**

The next one did not work for me. I don't understand why:  
**$regexp(%\_filename%,'['%genre%']' %title%,'['%genre%']' %title% {%publisher%})**

edit:  
Now I understand partly. This works:  
**$regexp(%\_filename%,'['%genre%']' %title%,'['%genre%']' %title% {%publisher%})**

---

<div class="post-metadata">

### Author: ![DetlevD](https://community.mp3tag.de/user_avatar/community.mp3tag.de/detlevd/32/123_2.png) [@DetlevD](https://community.mp3tag.de/u/DetlevD)
#### Post date: [April 26, 2011, 3:25pm UTC](https://community.mp3tag.de/t/file-rename-with-regexp-and-tag-contents-need-help/11920/6 "2011-04-26T15:25:27Z")

</div>

> [@anon40688970](#):
>
> ...Ah, interesting way to get the %publisher% into the regexp. ...

Hmm ... this might work too ...

**$replace(%\_filename%,'['%GENRE%'] '%TITLE%,'['%GENRE%'] '%TITLE%' {'%PUBLISHER%'}')**

From:  
"T:\TEST[genre] title.mp3"  
To:  
"T:\TEST[genre] title {publisher}.mp3"

DD.20110426.1928.CEST

---

<div class="post-metadata">

### Author: ![DetlevD](https://community.mp3tag.de/user_avatar/community.mp3tag.de/detlevd/32/123_2.png) [@DetlevD](https://community.mp3tag.de/u/DetlevD)
#### Post date: [April 26, 2011, 3:30pm UTC](https://community.mp3tag.de/t/file-rename-with-regexp-and-tag-contents-need-help/11920/7 "2011-04-26T15:30:47Z")

</div>

> [@pone](#):
>
> ... The next one did not work for me. I don't understand why:  
> **$regexp(%\_filename%,'['%genre%']' %title%,'['%genre%']' %title% {%publisher%})**

See post #2 in this thread.

DD.20110426.1933.CEST

---

<div class="post-metadata">

### Author: ![pone](https://community.mp3tag.de/user_avatar/community.mp3tag.de/pone/32/272_2.png) [@pone](https://community.mp3tag.de/u/pone)
#### Post date: [April 26, 2011, 3:36pm UTC](https://community.mp3tag.de/t/file-rename-with-regexp-and-tag-contents-need-help/11920/8 "2011-04-26T15:36:17Z")

</div>

> [@DetlevD](#):
>
> See post #2 in this thread.
> 
> DD.20110426.1933.CEST

what do you mean with you are not aware? it is possible.  
I solve my problem. $regexp needs '[' to escape [, $replace only needs '['. I edited my post (post #5 in this thread).

---

<div class="post-metadata">

### Author: ![anon40688970](https://community.mp3tag.de/letter_avatar_proxy/v4/letter/a/898d66/32.png) [@anon40688970](https://community.mp3tag.de/u/anon40688970)
#### Post date: [April 26, 2011, 3:41pm UTC](https://community.mp3tag.de/t/file-rename-with-regexp-and-tag-contents-need-help/11920/9 "2011-04-26T15:41:27Z")

</div>

In my (special) case i can not rely on the content of %title% and %genre% from the id3 tag. I used the words title and genre just to illustrate how the filename is composed. To get a more real life description. That's why i can not use replace in the way outlined from pone, right? And as Detlef pointed out, it is not possible to use field substitution in the replacement part of $regexp.

However, Detlef's solution does exactly what i was looking for.

Thank you both for your (always) immediate and insightful answers.

---

<div class="post-metadata">

### Author: ![DetlevD](https://community.mp3tag.de/user_avatar/community.mp3tag.de/detlevd/32/123_2.png) [@DetlevD](https://community.mp3tag.de/u/DetlevD)
#### Post date: [April 26, 2011, 3:46pm UTC](https://community.mp3tag.de/t/file-rename-with-regexp-and-tag-contents-need-help/11920/10 "2011-04-26T15:46:44Z")

</div>

> [@pone](#):
>
> ... what do you mean with you are not aware? it is possible. ...

**$regexp(%\_filename%,'\['%GENRE%'\] '%TITLE%,'['%GENRE%'] '%TITLE%' {'%PUBLISHER%'}')**

Oh yes, it works obviously!  
But it doesn't work a few minutes ago, maybe because of the hurry, my bad.  
Do I learned something new? I do not know for sure. ![:unsure:](https://community.mp3tag.de/uploads/default/original/1X/cfaeba84fe6704387c5b34dfdd8172bd51b6f533.gif ":unsure:")

But you have to be careful!  
There might be characters in the tag-fields, which need further escaping, when used as the regexp pattern argument?!?!

DD.20110426.1954.CEST

---

<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:23pm UTC](https://community.mp3tag.de/t/file-rename-with-regexp-and-tag-contents-need-help/11920/11 "2026-02-09T12:23:03Z")

</div>


