# Execute regular expression only if brackets present

**URL:** https://community.mp3tag.de/t/execute-regular-expression-only-if-brackets-present/68639
**Category:** Support
**Created:** [May 28, 2025, 12:24pm UTC](https://community.mp3tag.de/t/execute-regular-expression-only-if-brackets-present/68639 "2025-05-28T12:24:54Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![viviancels](https://community.mp3tag.de/letter_avatar_proxy/v4/letter/v/bbe5ce/32.png) [@viviancels](https://community.mp3tag.de/u/viviancels)
#### Post date: [May 28, 2025, 12:24pm UTC](https://community.mp3tag.de/t/execute-regular-expression-only-if-brackets-present/68639/1 "2025-05-28T12:24:54Z")

</div>

Hello. I have trouble with `$if(x,y,z)`  
Often the artist is indicated in the title of the track, you need to transfer it to the artist tag. It can be done this way. Example:  
Andy Moor - Trespass (feat. Sue McLaren) (Masoud Chill Out Mix)  
Action format value:  
Field: `ARTIST`  
Format string: `$if($regexp(%title%,'.*?\((feat..*?)\).*?','$1')`  
But how to set the condition that the action is performed only if there are brackets with the performer in title. If you apply this action in the case when there are no brackets with the artist in the title, then the title is copied to the artist's name. How to make sure that if there are no brackets with the performer in the track title, the action is not performed?

---

<div class="post-metadata">

### Author: ![ohrenkino](https://community.mp3tag.de/user_avatar/community.mp3tag.de/ohrenkino/32/4843_2.png) [@ohrenkino](https://community.mp3tag.de/u/ohrenkino)
#### Post date: [May 28, 2025, 12:31pm UTC](https://community.mp3tag.de/t/execute-regular-expression-only-if-brackets-present/68639/2 "2025-05-28T12:31:42Z")

</div>

Test for the presence of the brackets first:  
`$ifgreater($strstr(%title%,'('),0,....`

---

<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: [May 28, 2025, 2:02pm UTC](https://community.mp3tag.de/t/execute-regular-expression-only-if-brackets-present/68639/3 "2025-05-28T14:02:03Z")

</div>

> [@viviancels](#):
>
> But how to set the condition that the action is performed only if there are brackets with the performer шт title.

I'm just curious:  
How should Mp3tag distinguish between the performer names "Sue Mclaren" and "Masoud"?  
Are you sure that the performer name that you want to be transfered from TITLE to ARTIST exists always and only in the first pair of brackets following the word _feat._ ?

BTW: Your above regular expression `$if($regexp...` doesn't work for me with your example. It does not extract anything from the TITLE field to the ARTIST field. It would also overwrite whatever currently exists in ARTIST.

Something like  
`$ifgreater($strstr(%title%,'('),0,$regexp(%title%,'.*?\((feat..*?)\).*?','$1'),WhatElse)`  
could basically work and check for the position of an opening bracket.  
If such a position is greater then 0 (= opening bracket exists in TITLE) then the regular expression would be executed. If the position is 0 (= opening bracket does not exist in TITLE), then _WhatElse_ would be used.

---

<div class="post-metadata">

### Author: ![viviancels](https://community.mp3tag.de/letter_avatar_proxy/v4/letter/v/bbe5ce/32.png) [@viviancels](https://community.mp3tag.de/u/viviancels)
#### Post date: [May 29, 2025, 3:27am UTC](https://community.mp3tag.de/t/execute-regular-expression-only-if-brackets-present/68639/5 "2025-05-29T03:27:37Z")

</div>

Thank you. I found the solution to copie performer from TITLE to ARTIST:  
Action 1: Format value:  
Field: `ARTIST`  
Format string: `%artist% $regexp(%title%,'.*?\(?(feat..*?)\)?(\s\(.*?)?$|$','$1')`  
It works in cases:

```auto
Trespass (feat. Sue McLaren) (Masoud Chill Out Mix)
Trespass feat. Sue McLaren
Trespass (Masoud Chill Out Mix) (feat. Sue McLaren)
Trespass (Masoud Chill Out Mix) feat. Sue McLaren

```

**The next two actions delete preformer from TITLE and shall be arranged one after the other strictly in this order**

Action 2: Replace with regular expressions:  
Field: `TITLE`  
Regular explression: `\(*feat. .*?\)* \(`  
Replace matches with: `(`

Action 3: Replace with regular expressions:  
Field: `TITLE`  
Regular explression: ` \(*feat. .*?\)*$`  
Replace matches with:

 ![2025-05-29-092712](https://community.mp3tag.de/uploads/default/original/3X/d/5/d54fa50b1aa6e6dc2c7c58ec78b1b6dbc928e905.png)  
Probably, ACTION 2 and ACTION 3 can be combined into one common ACTION using |, but the problem is that in ACTION 2 there is a replacement for (, and in ACTION 3 a replacement for nothing

Finaly, the problem present in cases where the performer is not specified in TITLE, in such cases TITLE is completely copied to ARTIST 😬

---

<div class="post-metadata">

### Author: ![Casual\_Tea](https://community.mp3tag.de/user_avatar/community.mp3tag.de/casual_tea/32/18886_2.png) [@Casual\_Tea](https://community.mp3tag.de/u/Casual_Tea)
#### Post date: [May 29, 2025, 3:14pm UTC](https://community.mp3tag.de/t/execute-regular-expression-only-if-brackets-present/68639/6 "2025-05-29T15:14:44Z")

</div>

> [@viviancels](#):
>
> But how to set the condition that the action is performed only if there are brackets with the performer in title.

You can do this by using the regex twice in your action. First to check if the match is not equal to the field that you match on (brackets are present) and then to perform the actual replacement that you wish to perform if they are not equal. If they are equal, the current content of the field is retained. This works because the regex returns the field it is matched against in the case that no match was found by default.

It's quite wordy and I really wish there was a simple way to say: Abort the action/change nothing if the regex does not match.

I have described the problem and the workaround I'm using [in this thread](https://community.mp3tag.de/t/simple-way-to-abort-action-if-regex-does-not-match/66243).

Here's my latest example of this workaround:  
Action type: Format value  
Field:

```auto
MUSICBRAINZ_ALBUMTYPE

```

Format string:

```auto
$if($neql($regexp(%ALBUM%,.* \'['EP\']'.*,EP,0),%ALBUM%),$regexp(%ALBUM%,.* \'['EP\']'.*,EP,0),$meta_sep(MUSICBRAINZ_ALBUMTYPE,\\))

```

This action checks if `ALBUM` contains ` [EP]` and sets the `MUSICBRAINZ_ALBUMTYPE` to `EP` if it does. If `ALBUM` does not contain ` [EP]`, the release type is set to its current value(s).

---

<div class="post-metadata">

### Author: ![viviancels](https://community.mp3tag.de/letter_avatar_proxy/v4/letter/v/bbe5ce/32.png) [@viviancels](https://community.mp3tag.de/u/viviancels)
#### Post date: [May 31, 2025, 3:48am UTC](https://community.mp3tag.de/t/execute-regular-expression-only-if-brackets-present/68639/7 "2025-05-31T03:48:39Z")

</div>

Why are there 3 commas here?:

`$regexp(%ALBUM%,.* \'['EP\']'.*,EP,0)`  
I thought the formula should have this scheme:  
$regexp(%...%,'...','...')  
What does 0 mean?

I am confused  
TITLE: `Blue Way feat. Anna (Aerotek Remix)`  
ARTIST:`Amir Farhoodi`  
Format value:  
Field:`ARTIST`  
Format string:`$if($eql($regexp(%title%,'.*?\(?(feat..*?)\)?(\s\(.*?)?$|$','$1'),%title%),%artist% $regexp(%title%,'.*?\(?(feat..*?)\)?(\s\(.*?)?$|$','$1'),%artist%)`  
Why`$eql($regexp(%title%,'.*?\(?(feat..*?)\)?(\s\(.*?)?$|$','$1'),%title%)` returns false (not true) and nothing happens, %artist% copies to %artist%?  
But `$neql($regexp(%title%,'.*?\(?(feat..*?)\)?(\s\(.*?)?$|$','$1'),%title%)` returns true and `%artist% $regexp(%title%,'.*?\(?(feat..*?)\)?(\s\(.*?)?$|$','$1')` copies to %artist%?

I think mistake is somewhere here: `$eql($regexp(%title%,'.*?\(?(feat..*?)\)?(\s\(.*?)?$|$','$1'),%title%)`  
I don't fully understand how to specify equality of the first value `$regexp(%title%,'.*?\(?(feat..*?)\)?(\s\(.*?)?$|$','$1')` with the second value `%title%`

---

<div class="post-metadata">

### Author: ![Casual\_Tea](https://community.mp3tag.de/user_avatar/community.mp3tag.de/casual_tea/32/18886_2.png) [@Casual\_Tea](https://community.mp3tag.de/u/Casual_Tea)
#### Post date: [May 31, 2025, 11:39am UTC](https://community.mp3tag.de/t/execute-regular-expression-only-if-brackets-present/68639/8 "2025-05-31T11:39:02Z")

</div>

> [@viviancels](#):
>
> I thought the formula should have this scheme:  
> $regexp(%...%,'...','...')  
> What does 0 mean?

As you can see [here](https://docs.mp3tag.de/scripting/):

> `$regexp(x,e,r,c)` replaces the pattern specified by the [regular expression](https://docs.mp3tag.de/regex) e in the string x by r. The optional fourth parameter c enables ignore case (`1`) or disables the ignore case setting (`0`). Please note that you have to escape comma and other special characters in e.

The 0 simply means that the match is case sensitive (wich it already is by default, but it also doesn't hurt to explicitly state it).

So far I had not checked your regex and I'm not sure that I understand what you want to achieve.

> [@LyricsLover](#):
>
> It would also overwrite whatever currently exists in ARTIST.

In this example:

> [@viviancels](#):
>
> TITLE: `Blue Way feat. Anna (Aerotek Remix)`  
> ARTIST:`Amir Farhoodi`

With your regex:

> [@viviancels](#):
>
> `$regexp(%title%,'.*?\(?(feat..*?)\)?(\s\(.*?)?$|$','$1')`

`Amir Farhoodi` would get replaced by `feat. Anna`. Wouldn't it make more sense to append `feat. Anna` to get `Amir Farhoodi feat. Anna` as the result?

You should also note that you have to escape `.` if you want to match a literal dot like this `\.`.

```auto
$if($neql($regexp(%TITLE%,'^.*?\(?(feat.*?)\)?(?:\s\(.*?)?$',$1,0),%TITLE%),$regexp(%TITLE%,'^.*?\(?(feat.*?)\)?(?:\s\(.*?)?$',%ARTIST% $1,0),$meta_sep(ARTIST,\\))

```

After changing up your regex a bit and fixing the syntax, this one does append ` feat. Anna` to the artist for your given example.

Do note that it will append the artist again, should you re-run the action on its own.  
After running it twice you'd have `Amir Farhoodi feat. Anna feat. Anna`.

---

<div class="post-metadata">

### Author: ![viviancels](https://community.mp3tag.de/letter_avatar_proxy/v4/letter/v/bbe5ce/32.png) [@viviancels](https://community.mp3tag.de/u/viviancels)
#### Post date: [May 31, 2025, 1:31pm UTC](https://community.mp3tag.de/t/execute-regular-expression-only-if-brackets-present/68639/9 "2025-05-31T13:31:06Z")

</div>

> [@Casual\_Tea](#):
>
> Wouldn't it make more sense to append `feat. Anna` to get `Amir Farhoodi feat. Anna` as the result?

Yes, this job does first action:  
ARTIST  
`%artist% $regexp(%title%,'.*?\(?(feat\.?.*?)\)?(\s\(.*?)?$|$','$1')`  
This action works on its own when performed separately (no convention or other), it copies **feat. Anna** from TITLE to ARTIST  
Then one of two actions (depending on whether **feat. Anna** is located in front of the bracket or at the end of the TITLE) remove **feat. Anna** from TITLE

The goal is that the first action is performed only if the performer is in TITLE with:  
`$if($eql($regexp(%title%,'.*?\(?(feat.*?)\)?(\s\(.*?)?$|$','$1'),%title%),%artist% $regexp(%title%,'.*?\(?(feat..*?)\)?(\s\(.*?)?$|$','$1'),%artist%)`  
But it does not work yet, because $eql does not return TRUE, I don't know why

And finally, then you need to work on combining 2 and 3 actions into one action, I think this can be done with the something like that: `$if($eql(1,2,$if($eql(1,2,%artist%))`

---

<div class="post-metadata">

### Author: ![viviancels](https://community.mp3tag.de/letter_avatar_proxy/v4/letter/v/bbe5ce/32.png) [@viviancels](https://community.mp3tag.de/u/viviancels)
#### Post date: [May 31, 2025, 2:02pm UTC](https://community.mp3tag.de/t/execute-regular-expression-only-if-brackets-present/68639/10 "2025-05-31T14:02:08Z")

</div>

> [@viviancels](#):
>
> But it does not work yet, because $eql does not return TRUE, I don't know why

Found the mistake:  
`$if($eql($regexp(%title%,'.*?\(?(feat.*?)\)?(\s\(.*?)?$|$','$1'),%title%),%artist% $regexp(%title%,'.*?\(?(feat..*?)\)?(\s\(.*?)?$|$','$1'),%artist%)`  
Must be:  
`$if($eql($regexp(%title%,'.*?\(?(feat.*?)\)?(\s\(.*?)?$|$','$1')=%title%),%artist% $regexp(%title%,'.*?\(?(feat..*?)\)?(\s\(.*?)?$|$','$1'),%artist%)`

But now it copies full TITLE to ARTIST if there is no \*\*feat \*\*in TITLE

---

<div class="post-metadata">

### Author: ![ohrenkino](https://community.mp3tag.de/user_avatar/community.mp3tag.de/ohrenkino/32/4843_2.png) [@ohrenkino](https://community.mp3tag.de/u/ohrenkino)
#### Post date: [May 31, 2025, 2:11pm UTC](https://community.mp3tag.de/t/execute-regular-expression-only-if-brackets-present/68639/11 "2025-05-31T14:11:30Z")

</div>

It's a pity really that you never reacted to @LyricsLover's and my suggestion.  
You can test a format string (including regular expressions) with the preview in Convert\>Tag-Tag.  
That preview may also help to develop the full expression step by step.

---

<div class="post-metadata">

### Author: ![viviancels](https://community.mp3tag.de/letter_avatar_proxy/v4/letter/v/bbe5ce/32.png) [@viviancels](https://community.mp3tag.de/u/viviancels)
#### Post date: [May 31, 2025, 2:32pm UTC](https://community.mp3tag.de/t/execute-regular-expression-only-if-brackets-present/68639/12 "2025-05-31T14:32:17Z")

</div>

Thank you, I will test. I apologize for deviating from the topic

---

<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: [July 4, 2025, 7:11am UTC](https://community.mp3tag.de/t/execute-regular-expression-only-if-brackets-present/68639/13 "2025-07-04T07:11:07Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
