* Feature Request * Occurrence option on Replace action

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.

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

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

This would be for both Windows and MacOS.

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.

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"


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.

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)
$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.

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

1 Like

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.

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.

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

Thanks I will look into this.

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.

1 Like

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?

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.

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.