I am processing files in order to replace a list of pre-defined keywords with a pre- and a post-string (say "#" and ".") like this :
"Word Word2 anotherWord and some other stuff" should become "#Word. #Word2. #anotherWord. and some other stuff"
My keys are unique and processed the keys from longest key to smallest, so I know inclusion can only be on already
However, if I have key inclusion (e.g. Word2
contains Word
), and if I do
"Word Word2 anotherWord and some other stuff"
.Replace("anotherWord", "#anotherWord.")
.Replace("Word2", "#Word2.")
.Replace("Word", "#Word.")
I get the following result:
"#Word. ##Word.2. #another#Word.. and some other stuff"
For sure, my approach isn't wokring. So what is the way to make sure I only replace a key in the string, if it is NOT contained in another key? I tried RegExp but didn't find the correct way. Or there is another solution?