File rename with regexp and tag contents, need help

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

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.


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


You can start to play with this expression ...

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

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


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.

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%})

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

Hmm ... this might work too ...

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

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


See post #2 in this thread.


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

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.

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

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?!?!