# \* Feature Request \* Occurrence option on Replace action

**URL:** https://community.mp3tag.de/t/feature-request-occurrence-option-on-replace-action/63699
**Category:** General Discussion
**Created:** [February 6, 2024, 2:03pm UTC](https://community.mp3tag.de/t/feature-request-occurrence-option-on-replace-action/63699 "2024-02-06T14:03:11Z")
**Posts on this page:** 16
**Page:** 1

<div class="post-metadata">

### Author: ![djquartz](https://community.mp3tag.de/user_avatar/community.mp3tag.de/djquartz/32/12233_2.png) [@djquartz](https://community.mp3tag.de/u/djquartz)
#### Post date: [February 6, 2024, 2:03pm UTC](https://community.mp3tag.de/t/feature-request-occurrence-option-on-replace-action/63699/1 "2024-02-06T14:03:11Z")

</div>

I've been thinking about how to solve the issue of stopping all commas from being replaced by 'feat.' in my action definition.

If we had an occurrence option with say 1 - 5 as the selection. Then it would only run that action based on how many times it found a match.

ie: If I only wanted to replace one comma found per track I could set this to 1 and it would only execute this once per track based on the matched criteria.

---

<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: [February 6, 2024, 2:41pm UTC](https://community.mp3tag.de/t/feature-request-occurrence-option-on-replace-action/63699/2 "2024-02-06T14:41:57Z")

</div>

In many cases this use case can be tackled by a regular expression.  
see e.g. here:

> [@Action Definition Question replace first comma with feat](https://community.mp3tag.de/t/action-definition-question-replace-first-comma-with-feat/61534/):
>
> This is going to sound like a weird one, but I'm not sure how to fix this one. I have an action that I use both in Mac and Windows versions called, 'Comma replace with feat.' What is does is find a comma followed by a space and replaces it with feat. It works but it replaces all instances of this. So I have to manually go and replace second and third instances with , I'm trying to emulate the function from from my old tagger because it would only replace the first found instance in every f…

and as you posted in the Mac section before: does this request aim at the Mac version?

---

<div class="post-metadata">

### Author: ![djquartz](https://community.mp3tag.de/user_avatar/community.mp3tag.de/djquartz/32/12233_2.png) [@djquartz](https://community.mp3tag.de/u/djquartz)
#### Post date: [February 6, 2024, 2:53pm UTC](https://community.mp3tag.de/t/feature-request-occurrence-option-on-replace-action/63699/3 "2024-02-06T14:53:33Z")

</div>

This would be for both Windows and MacOS.

---

<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: [February 6, 2024, 3:09pm UTC](https://community.mp3tag.de/t/feature-request-occurrence-option-on-replace-action/63699/4 "2024-02-06T15:09:51Z")

</div>

If you want to replace all occurences of a string, then use a simple replace.  
If you want to replace only those matching a certain pattern, use replace wth a regular expression.  
It would be up to the user to determine a valid pattern - just like it would be up to the user to determine, how many times an attempt should be made to replace a string.

---

<div class="post-metadata">

### Author: ![mista\_j](https://community.mp3tag.de/user_avatar/community.mp3tag.de/mista_j/32/15808_2.png) [@mista\_j](https://community.mp3tag.de/u/mista_j)
#### Post date: [February 7, 2024, 1:34pm UTC](https://community.mp3tag.de/t/feature-request-occurrence-option-on-replace-action/63699/5 "2024-02-07T13:34:18Z")

</div>

Not sure about you but i don't know how to use regex properly so I made a longer roundabout way of doing this using just what's available under the scripting functions on the main website.

Artist 1, Artist 2, Artist 3, Artist 4

In the example above i've made a formula that would change the last comma to the & sign.

$IF($AND($NOT($GRTR($strstr(%Artist%'&'),0)),$NOT($GRTR($strstr(%Artist%'Ft.'),0))),$cutright(%Artist%,$SUB($ADD(1,$Len(%Artist%)),$strrchr(%Artist%,','))) $Replace($cutleft(%Artist%,$SUB($strrchr(%Artist%,','),1)),',','&'),%Artist%)

So i started with what my rules were firstly the Artist field can't already have an & or Ft. because I'd probably risk duplicating the & or having a misplaced one.

These rules state that the "String" & as well as Ft. are not present in the Artist Field  
It basically returns the character number for the first occurrence of that string in the field specified, if it's never found the returned number is 0 which is why the (grtr) greater function is also used here. If the first occurrence is not greater than 0 then it must not exist within the string "Artist"

$NOT($GRTR($strstr(%Artist%'&'),0))  
$NOT($GRTR($strstr(%Artist%'Ft.'),0))

Next I have some variables I will need to do some simple math  
$strrchr(%Artist%,',') - Where is the last comma  
$Len(%Artist%) - count the character length of the whole artist field

I then used the cutleft & cutright in conjunction with some add / subtract to do a replacement within a subset of the Artist field. I basically use the cutleft / right to isolate part of the artist & then I can replace just one of the commas instead of all of them.

If I know where the last comma is & I get rid of everything in front of the last comma that means all thats left is ", Artist 4" then I replace the comma from this substring & join it wilth its opposite substring of the original artist which would be "Artist 1, Artist 2, Artist 3" Note I added a space between the two in the formula.

I hope I have explained this well enough to follow. Whenever I come across issues I have to use my lack of regex knowledge to come up with something creatively convoluted.

---

<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: [February 7, 2024, 1:48pm UTC](https://community.mp3tag.de/t/feature-request-occurrence-option-on-replace-action/63699/6 "2024-02-07T13:48:11Z")

</div>

> [@mista\_j](#):
>
> Artist 1, Artist 2, Artist 3, Artist 4

To replace the last comma with an &, try:  
`$regexp('Artist 1, Artist 2, Artist 3, Artist 4','(.*), (.*)',$1 & $2)`

If you are not sure whether you have already inserted an &, then use  
`$regexp($replace('Artist 1, Artist 2, Artist 3, Artist 4', & ,', '),'(.*), (.*)',$1 & $2)`  
or  
`$regexp($replace('Artist 1, Artist 2, Artist 3 & Artist 4', & ,', '),'(.*), (.*)',$1 & $2)`  
the outcome will be in both cases:  
Artist 1, Artist 2, Artist 3 & Artist 4

You can test a regular expression in the preview of Convert\>Tag-Tag.

---

<div class="post-metadata">

### Author: ![mista\_j](https://community.mp3tag.de/user_avatar/community.mp3tag.de/mista_j/32/15808_2.png) [@mista\_j](https://community.mp3tag.de/u/mista_j)
#### Post date: [February 8, 2024, 6:23am UTC](https://community.mp3tag.de/t/feature-request-occurrence-option-on-replace-action/63699/7 "2024-02-08T06:23:34Z")

</div>

Oo. Thank you. I need to learn regular expression because that seems much simpler than everything i went through

---

<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: [February 8, 2024, 6:35am UTC](https://community.mp3tag.de/t/feature-request-occurrence-option-on-replace-action/63699/8 "2024-02-08T06:35:33Z")

</div>

Thank you, too.  
So the summary is: until such an occurance option is implemented, use normal replace or replace with regular expressions, both functions which are already available.

---

<div class="post-metadata">

### Author: ![djquartz](https://community.mp3tag.de/user_avatar/community.mp3tag.de/djquartz/32/12233_2.png) [@djquartz](https://community.mp3tag.de/u/djquartz)
#### Post date: [February 8, 2024, 10:07am UTC](https://community.mp3tag.de/t/feature-request-occurrence-option-on-replace-action/63699/9 "2024-02-08T10:07:57Z")

</div>

To add context to this.

The problem wasn't that I couldn't replace the commas. I'm currently using the replace group command.

It looks for comma, space (, ) to be replaced in the filename and artist fields with 'feat.'

It's works but replaces all instances found. Currently I just want the first instance to be replaced.

---

<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: [February 8, 2024, 12:23pm UTC](https://community.mp3tag.de/t/feature-request-occurrence-option-on-replace-action/63699/10 "2024-02-08T12:23:23Z")

</div>

> [@djquartz](#):
>
> Currently I just want the first instance to be replaced.

Try an action of the type "Format value" for ARTIST  
Format string: `$regexp(%artist%,'(.*?), (.*)',$1 feat. $2)`

---

<div class="post-metadata">

### Author: ![djquartz](https://community.mp3tag.de/user_avatar/community.mp3tag.de/djquartz/32/12233_2.png) [@djquartz](https://community.mp3tag.de/u/djquartz)
#### Post date: [February 8, 2024, 6:08pm UTC](https://community.mp3tag.de/t/feature-request-occurrence-option-on-replace-action/63699/11 "2024-02-08T18:08:51Z")

</div>

Thanks I will look into this.

---

<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: [February 8, 2024, 7:22pm UTC](https://community.mp3tag.de/t/feature-request-occurrence-option-on-replace-action/63699/12 "2024-02-08T19:22:38Z")

</div>

> [@Tool to visualize (and explain) regular expressions?](https://community.mp3tag.de/t/tool-to-visualize-and-explain-regular-expressions/62328):
>
> Out of curiosity, did you make that visualization yourself or are you using a tool I'm not aware of? Looks really neat to quickly understand a more complicated regex.

This thread mentions a few helpful sites that can assist you in visualizing and understanding regular expressions.  
I love regex and wish I had learned it 15 years earlier than I did. It would've saved my poor hands hundreds of thousands of key presses and myself hundreds if not thousands of hours of manual work. I always had the nagging feeling at the back of my mind "there's a pattern, there must be an easier way to do this".  
Well, there was.

---

<div class="post-metadata">

### Author: ![djquartz](https://community.mp3tag.de/user_avatar/community.mp3tag.de/djquartz/32/12233_2.png) [@djquartz](https://community.mp3tag.de/u/djquartz)
#### Post date: [February 9, 2024, 7:53am UTC](https://community.mp3tag.de/t/feature-request-occurrence-option-on-replace-action/63699/13 "2024-02-09T07:53:48Z")

</div>

This worked perfectly!

Then I just convert using tag - filename option to rename the files correctly.

Unless there is a way to make it work with the \_FILENAME attribute?

---

<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: [February 9, 2024, 8:08am UTC](https://community.mp3tag.de/t/feature-request-occurrence-option-on-replace-action/63699/14 "2024-02-09T08:08:49Z")

</div>

> [@djquartz](#):
>
> Then I just convert using tag - filename option to rename the files correctly.

Yes, that is the safest way.  
OF course, you could try the same expression for \_FILENAME and  
Format string: `$regexp(%_filename%,'(.*?), (.*)',$1 feat. $2)`  
but the results will depend a lot on the structure of the filename. E.g. if the title data comes first, then the expression will take the first comma from that part.  
But as Convert\>Tag-Tag (which is the converter equivalent to "Format value") has a preview, you can test it first.

---

<div class="post-metadata">

### Author: ![djquartz](https://community.mp3tag.de/user_avatar/community.mp3tag.de/djquartz/32/12233_2.png) [@djquartz](https://community.mp3tag.de/u/djquartz)
#### Post date: [February 9, 2024, 8:11am UTC](https://community.mp3tag.de/t/feature-request-occurrence-option-on-replace-action/63699/15 "2024-02-09T08:11:55Z")

</div>

Right, that's what I was thinking. In a perfect world it would work but not always.

I'll just use the convert tag - filename option to rename the files.

It's the safest way.

---

<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: [March 10, 2024, 8:12am UTC](https://community.mp3tag.de/t/feature-request-occurrence-option-on-replace-action/63699/16 "2024-03-10T08:12:45Z")

</div>

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