# Regex to remove duplicate strings in a field

**URL:** https://community.mp3tag.de/t/regex-to-remove-duplicate-strings-in-a-field/65926
**Category:** Support
**Created:** [September 13, 2024, 7:13pm UTC](https://community.mp3tag.de/t/regex-to-remove-duplicate-strings-in-a-field/65926 "2024-09-13T19:13:59Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![lkantrov](https://community.mp3tag.de/letter_avatar_proxy/v4/letter/l/edb3f5/32.png) [@lkantrov](https://community.mp3tag.de/u/lkantrov)
#### Post date: [September 13, 2024, 7:13pm UTC](https://community.mp3tag.de/t/regex-to-remove-duplicate-strings-in-a-field/65926/1 "2024-09-13T19:13:59Z")

</div>

I'm trying to remove duplicate strings in a field. The values are comma separated like this:

> Abc, xyz, DeF, lmno, zebra, monkey, xyz, lmno, Abc

There can be anywhere from 0 to many (~20) items in the list.

I'd like a regex to remove duplicates.

So

> Abc, xyz, DeF, lmno, zebra, monkey, xyz, lmno, Abc  
> becomes  
> Abc, xyz, DeF, lmno, zebra, monkey  
> or  
> DeF, zebra, monkey, xyz, lmno, Abc

Thanks for any suggestions!

---

<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: [September 13, 2024, 7:20pm UTC](https://community.mp3tag.de/t/regex-to-remove-duplicate-strings-in-a-field/65926/2 "2024-09-13T19:20:57Z")

</div>

> [@lkantrov](#):
>
> Thanks for any suggestions!

See e.g. here:

> [@Delete repeated genre's in the same exact field](https://community.mp3tag.de/t/delete-repeated-genres-in-the-same-exact-field/58954):
>
> I have looked everywhere for a solution, and have found nothing to pre-exist. All the other posts anywhere close to this either deal with multiple fields (I have just one field) named Genre, don't work, or removes everything after the first \\'s, and are several years old posts. I have ONE genre field (not multiple,) and want to cut out duplicates out of the same (not multiple) fields. I can cut it down to this so that every word is separated and ready for it to be applied; …

---

<div class="post-metadata">

### Author: ![lkantrov](https://community.mp3tag.de/letter_avatar_proxy/v4/letter/l/edb3f5/32.png) [@lkantrov](https://community.mp3tag.de/u/lkantrov)
#### Post date: [September 17, 2024, 4:16am UTC](https://community.mp3tag.de/t/regex-to-remove-duplicate-strings-in-a-field/65926/3 "2024-09-17T04:16:02Z")

</div>

Thanks @ohrenkino for the pointer. It wasn't a 100% solution, but it put me on a path that was helpful. The uber-nested regex is a bear to understand...I used Perplexity AI to help me understand what it was doing at a high level, and then dusted off my debugging skills, read a lot of reference stuff and eventually sorted out what I needed.

My goal was to de-dupe a list of comma separated keywords. Many of the words are in CamelCase (e.g. DancePop, NewWave, PopRock).

Perplexity gave me a few suggestions, but the code it provided didn't work perfectly in MP3Tag/Boost.

Here's what did work eventually:

Action type: Format Value  
Field: ContentGroup  
Format String:

> $regexp($reverse($regexp($reverse($regexp($regexp(%ContentGroup%,'^(\s+,_|\s_,+)+|(\s+,_|\s_,+)+$|((?\<=,)\s+)|(\s+(?=,))',' '),'\b([^,\s]+(?:\s+[^,\s]+)_)\s_,\s\*(?=.\*\b\1\b)',)),'^,+|,+$',' ')),'(?\<=,)',)

One of the challenges I had with another action I found was it would take input like:  
Pop, Rock, PopRock, Instrumental  
and would see the "Rock," on the end of PopRock as the same as the Rock that occurred earlier in the field...and delete one of the values.

My solution looks for word boundaries to avoid that issue.

Hope this helps others.

Many thanks to @Florian and the community here for such a great tool and sharing of knowledge.

---

<div class="post-metadata">

### Author: ![lkantrov](https://community.mp3tag.de/letter_avatar_proxy/v4/letter/l/edb3f5/32.png) [@lkantrov](https://community.mp3tag.de/u/lkantrov)
#### Post date: [September 17, 2024, 4:16am UTC](https://community.mp3tag.de/t/regex-to-remove-duplicate-strings-in-a-field/65926/4 "2024-09-17T04:16:30Z")

</div>

![DS037](https://community.mp3tag.de/uploads/default/original/3X/0/a/0a896d1a2f2f28a6deffb8e9d923e7b54fadab43.png)

---

<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: [October 17, 2024, 4:16am UTC](https://community.mp3tag.de/t/regex-to-remove-duplicate-strings-in-a-field/65926/5 "2024-10-17T04:16:36Z")

</div>

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