I am using a Matcher to find specific patterns in a String that contains a text.
The regular expression checks for various patterns e.g. for simplicity:
\\bword1\\b|(\\b(?:word2(?:(\\s+)word3)?)?) etc
Anyway I can find the patterns in the string.
So far ok.
What I want though, is to be able to use the Matcher.replaceAll in order to replace each pattern found, with the same pattern but with some addition.
E.g.
If the matcher finds word1 somewhere, I want to replace it with {v}word1{v}.
How is this best done?I tried to add the groups that matcher finds in a list and then loop over the input string to replace all but obviously this did not work.
UPDATE:
I can match the terms I need and @Luis regex does the replace, but I need to replace all in my String.
By looping and doing the regex "pattern", "{v}$0{v}") inside the string I break previous occurences I made. I need to somehow include a negative lookbehind in the replaceAll so as to understand when a term has already been replaced
UPDATE:
Using @Luis regex and negative lookbehind it worked
UPDATED:
"pattern".replaceAll("pattern", "{v}$0{v}"))I’ve tested this and it returns “{v}pattern{v}” as desired.