# Replace a string, except if the string is preceded and followed by "x"

**URL:** <https://community.mp3tag.de/t/replace-a-string-except-if-the-string-is-preceded-and-followed-by-x/57568>\
**Category:** Support\
**Created:** [June 8, 2022, 6:52pm UTC](https://community.mp3tag.de/t/replace-a-string-except-if-the-string-is-preceded-and-followed-by-x/57568 "2022-06-08T18:52:27Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![SpirosG](https://community.mp3tag.de/user_avatar/community.mp3tag.de/spirosg/32/8589_2.png) [@SpirosG](https://community.mp3tag.de/u/SpirosG)\
**Post date:** [June 8, 2022, 6:52pm UTC](https://community.mp3tag.de/t/replace-a-string-except-if-the-string-is-preceded-and-followed-by-x/57568/1 "2022-06-08T18:52:27Z")

</div>

Hi,  
I want to create an action to replace a hyphen with a round bracket this is possible if i use a **Format Value** action `$regexp(%title%, - , '(')`, but i also want to exclude the occasions where there is a digit before and after the hyphen `\d - \d`.  
How will i accomplish that?

Thanks.

---

<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:** [June 8, 2022, 7:11pm UTC](https://community.mp3tag.de/t/replace-a-string-except-if-the-string-is-preceded-and-followed-by-x/57568/2 "2022-06-08T19:11:14Z")

</div>

You want to have one regular expression looking for a hyphen excluding the case  
\d space hyphen space \d  
Correct?

Then you could create a regular expression loooking for  
\D space hyphen space \D

The uppercase \D is a "non-digit". To be sure if this works, you should show us some real world examples.

---

<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:** [June 8, 2022, 7:17pm UTC](https://community.mp3tag.de/t/replace-a-string-except-if-the-string-is-preceded-and-followed-by-x/57568/3 "2022-06-08T19:17:11Z")

</div>

You could try ....  
Edit: the first was rubbish.

Edit 2:  
This one looks better:  
`$regexp(%title%,(.*)[^0-9] - [^0-9](.*),'$1' '(''$2')`  
transforms  
"Story of 6 - 6 Life" -\>  
"Story of 6 - 6 (Life"

---

<div class="post-metadata">

**Author:** ![SpirosG](https://community.mp3tag.de/user_avatar/community.mp3tag.de/spirosg/32/8589_2.png) [@SpirosG](https://community.mp3tag.de/u/SpirosG)\
**Post date:** [June 8, 2022, 7:40pm UTC](https://community.mp3tag.de/t/replace-a-string-except-if-the-string-is-preceded-and-followed-by-x/57568/4 "2022-06-08T19:40:08Z")

</div>

Hi @LyricsLover

Real example.

**Give It To Me - Vocal Club Edit (95 - 122 Transition)** and i want this to be renamed to **Give It To Me (Vocal Club Edit (95 - 122 Transition)**

If i use the `$regexp(%title%, - , '(')`, the result is **Give It To Me (Vocal Club Edit (95 (122 Transition)**

**_PS_. there is already an action to add the the missing round bracket at the end of the (Vocal Club Edit)**

If i use `$regexp(%title%,\D - \D, '(',)` then, the result is **Give It To M (ocal Club Edit (95 - 122 Transition)**

@ohrenkino if i use your regex, title remains the same as before.

---

<div class="post-metadata">

**Author:** ![SpirosG](https://community.mp3tag.de/user_avatar/community.mp3tag.de/spirosg/32/8589_2.png) [@SpirosG](https://community.mp3tag.de/u/SpirosG)\
**Post date:** [June 8, 2022, 7:44pm UTC](https://community.mp3tag.de/t/replace-a-string-except-if-the-string-is-preceded-and-followed-by-x/57568/5 "2022-06-08T19:44:05Z")

</div>

> [@ohrenkino](#):
>
> $regexp(%title%,(.\*)[^0-9] - ^0-9,'$1' '(''$2')

Nope, this is not correct.

If i apply this one, then happens the opposite of what i want.

**Give It To Me - Vocal Club Edit (95 (122 Transition)**

---

<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:** [June 8, 2022, 7:45pm UTC](https://community.mp3tag.de/t/replace-a-string-except-if-the-string-is-preceded-and-followed-by-x/57568/6 "2022-06-08T19:45:22Z")

</div>

If it is always the second hyphen that is to be left alone, then try:  
`$regexp('Give It To Me - Vocal Club Edit (95 - 122 Transition)',(.*?) - (.*),'$1' '(''$2')`

---

<div class="post-metadata">

**Author:** ![SpirosG](https://community.mp3tag.de/user_avatar/community.mp3tag.de/spirosg/32/8589_2.png) [@SpirosG](https://community.mp3tag.de/u/SpirosG)\
**Post date:** [June 8, 2022, 7:51pm UTC](https://community.mp3tag.de/t/replace-a-string-except-if-the-string-is-preceded-and-followed-by-x/57568/7 "2022-06-08T19:51:34Z")

</div>

> [@ohrenkino](#):
>
> $regexp('Give It To Me - Vocal Club Edit (95 - 122 Transition)',(._?) - (._),'$1' '(''$2')

This one works but i want to be sure that even if the `\d - \d` string is the first one will remain intact.

I know i could use a filter like `TITLE MATCHES " - " AND NOT TITLE MATCHES "\d - \d"` but i was wondering if there is a regular expression which would do the same.

---

<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:** [June 8, 2022, 7:55pm UTC](https://community.mp3tag.de/t/replace-a-string-except-if-the-string-is-preceded-and-followed-by-x/57568/8 "2022-06-08T19:55:32Z")

</div>

then the best way is still to filter for the files that should be treated.

---

<div class="post-metadata">

**Author:** ![SpirosG](https://community.mp3tag.de/user_avatar/community.mp3tag.de/spirosg/32/8589_2.png) [@SpirosG](https://community.mp3tag.de/u/SpirosG)\
**Post date:** [June 8, 2022, 7:58pm UTC](https://community.mp3tag.de/t/replace-a-string-except-if-the-string-is-preceded-and-followed-by-x/57568/9 "2022-06-08T19:58:18Z")

</div>

I see.  
I was typing almost the same 😅.

Thanks both of you @ohrenkino and @LyricsLover for the help.

---

<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:** [June 8, 2022, 9:20pm UTC](https://community.mp3tag.de/t/replace-a-string-except-if-the-string-is-preceded-and-followed-by-x/57568/10 "2022-06-08T21:20:44Z")

</div>

> [@SpirosG](#):
>
> If i use `$regexp(%title%,\D - \D, '(',)` then, the result is **Give It To M (ocal Club Edit (95 - 122 Transition)**

You could try it with  
`$regexp(%title%,(\D) - (\D),'$1 ($2'))`  
for your given example.

This would transform the content in %TITLE% from  
`Give It To Me - Vocal Club Edit (95 - 122 Transition)`  
into  
`Give It To Me (Vocal Club Edit (95 - 122 Transition))`

Please test it carefully with some more real world examples.

Short explanation:  
Search for a combination of  
any non-digit character (and put it into the captured group $1)  
followed by a space a hyphen a space  
followed by any non-digit character (and put it into the captured group $2)  
Then rearrange found combinations with the  
content of $1 followed by a space an opening round bracket and the  
content of $2  
then add a closing round bracket at the end

---

<div class="post-metadata">

**Author:** ![SpirosG](https://community.mp3tag.de/user_avatar/community.mp3tag.de/spirosg/32/8589_2.png) [@SpirosG](https://community.mp3tag.de/u/SpirosG)\
**Post date:** [June 11, 2022, 7:01pm UTC](https://community.mp3tag.de/t/replace-a-string-except-if-the-string-is-preceded-and-followed-by-x/57568/11 "2022-06-11T19:01:56Z")

</div>

Hey @LyricsLover i totally missed this one.  
I gave it some tries and it works, but i had to remove the last round bracket because otherwise i had a bracket at the end of every title, even those without any bracket at all.

The one that works for me is `$regexp(%title%,(\D) - (\D),'$1 ($2')`  
if i use this one, the title will transform to `Give It To Me (Vocal Club Edit (95 - 122 Transition)`  
afterwards i use another regular expression to close any open bracket if there is any.  
Then the title will be transformed to `Give It To Me (Vocal Club Edit) (95 - 122 Transition)`

Thanks a lot for the help.  
Appreciate it.

---

<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 11, 2022, 7:02pm UTC](https://community.mp3tag.de/t/replace-a-string-except-if-the-string-is-preceded-and-followed-by-x/57568/12 "2022-07-11T19:02:49Z")

</div>

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