Unable to use regex to get substring from directory name

I am working with a collection of music folders that have a catalog id somewhere in the folder name. I want to be able to get that id out of the directory name and save it to the catalog id field, and then later use it in a subsequent action step to add it to the album field in brackets. I am able to create a regex that works to identify the substring, but the same regex doesn't seem to work in this tool.

The position of the substring in the folder name is inconsistent, but the id substring follows this pattern:

K7 followed by numbers and capital letters followed by either a - or ) indicating the end of the id was the previous character


text text text K7123CD-text text  -> K7123CD
text text (K7234EP) text          -> K7234EP
text text text-K7456DDD-text      -> K7456DDD

The regex I am trying to use for this looks like this:


I tested that regex against the sample strings above and it found the right substring:

echo "text text text K7123CD-text text" | grep -P 'K7.+?(?=[-)])'
echo 'text text (K7234EP) text' | grep -P 'K7.+?(?=[-)])'
echo 'text text text-K7456DDD-text' | grep -P 'K7.+?(?=[-)])'

In mp3Tag, I tried creating an action like this:

Format value:
Format string: $regexp(%_directory%, K7.+?(?=[-)]) ,$1)

The resulting catalog id just looks like this:

It looks like it just took the last parameter of the function and used it as a string directly, which makes me think that something is not escaped properly, but there doesn't seem to be any documentation on how to escape characters in actions. I tried multiple variations with no luck.
Any ideas on this, or even better, direct and clear documentation on what is actually expected here? Thanks.

Even though clearity and directness are in the eye of the beholder, here is the help chapter on regular expressions:

You can test (regular) expression in the function Convert>Tag-Tag

An example would be:
$regexp('text text text (K7123CD) text text','.*(K.*?)[-|)].*',$1)

I've been using grep -P and regexr.com to build these regex match strings, but the behavior I am seeing between those and what occurs in mp3Tag seems to be different. Do you know why that is?

For example, if I take the regex you just posted and put it in regexr.com, it matches the entire string, not just the substring. grep -P also matches the entire string when I test with your regex like this.

echo 'text text text-K7456DDD-text' | grep -P '.*(K.*?)[-|)].*'

mp3tag doesn't seem to have a problem with the string though and just matches the substring I am looking for, which to me indicates that there's different versions of regex in play, or different interpretations of the search strings. I previously read the doc blurb you pointed to and did not see anything about quote requirements or why the regex behavior is different in mp3tag. This makes it difficult because most of the real world documentation seems to be talking about a different flavor of regex.

Also, FYI, the regex you posted returns the entire string if there is no match.

here is a statement about the flavour:

Yes, that is the way the regexp machine works in MP3tag

See the Help on scripting:
$regexp(x,expr,repl) replaces the pattern specified by the regular expression expr in the string x by repl . The fourth optional parameter enables ignore case (1) or disables the ignore case setting (0). Please note that you have to escape comma and other special characters in expr .