Help with replace - put word in square brackets

Hi all, i need to do this simple replacement of field, but i can't find how to do it:

Sample of two songs:

Original title song 1 " Macarena - Energy 5 "
Original title song 2 " One - Energy 3"

What i need, put between brackets "Energy x"

Replaced title song 1 " Macarena - [Energy 5]
Replaced title song 2 " One - [Energy 3]"

Until now i was replacing "Energy 3" for "[Energy 3]" but i have 7k songs, i need to automate the proccess.

Thanks in advance!!

You could use Convert Tag -> Tag with
Field:
TITLE
Format string:
$regexp(%TITLE%,(Energy \d+),'\[$1\]')

image

or use an Action from Type "Replace with regular expression"

The above regular expression searches for the word Energy followed by a number.
If found, it put square brackets around this found word and number.

Thank you so much, works like a charm.

may you tell me how to must config the action "replace with regex"? this way the config was saved as an action.

Thanks again

image

Documentation about Replace with regular expression

The advantage of Convert Tag -> Tag is the immediate preview and the complete preview (if you press the Preview button).

Sorry, this is my last question i swear xD.

Now with the action works too, but if the original title have Brackets, this action add another.
Sample

Original: [Energy 2]
after action: [[Energy 2]]
Can i make a "only add brackets when there isn't?
thanks again

Not that easy... :wink: **

Just filter first for titles which contains Energy without square brackets.
Press F3 to activate the filter and enter something like
NOT %TITLE% HAS [Energy AND %TITLE% HAS Energy
Then only use your Action to this filtered list of titles with Energy.

**
Please be aware that there are many combinations of Energy followed by a number that could occur:
+ with leading space
+ with any other leading character
+ without any character like MaximalEnergy 7
+ with trailing space
+ with any other trailing characters

You should carefully check if the above regular expression matches ALL your cases.
A previously applied filter highly reduce the risk to change TITLE content in a unwanted way.

I mean he could adjust it like this:
grafik

(Energy \d+)(?!\])

to only match when Energy number is not directly followed by a closing square bracket. However as you said, there are many possible cases and filtering should happen as well.

Yes, a search with a "negative lookahead" would be possible:

The more the OP wants this regular expression to be automated and reusable, the more strictly it must be specified.

Sometimes when I run into this type of situation instead of trying to write a more complicated regex/action to perfectly do what I want, I just create another action in the action group to fix the imperfect "fix". :slight_smile:

So in your example, instead of trying to arrive at an action that only add brackets when necessary, I'd add an action to clean up the double brackets by replacing the double brackets with single ones.

Here's an example of the actions you could add to the end of your original action group.

DS041

This has the benefit of being easy to understand and debug later as compared with a more complex regex that often are nearly unreadable unless you're a software programmer by day.

Hope that helps.

I initially thought up a regex that does something like that.

\[?(Energy \d+)\]?

matches Energy + optionally brackets around it.

My reasoning for not going that route is pointless computation (and yes, I do write programs/scripts so maybe I'm biased).
This regex "changes" the value of all instances of [Energy number] to the same data as it was before, which is a needless step.

With the negative lookahead of my regex, the computation stops when a "]" is found after "Energy number".
With how fast computers are these days it's likely irrelevant but I like to create clean and efficient solutions.

If you don't understand a regex, copy it into Chat GPT or another AI of your choice and it will in most cases explain to you very well how and why they work.