Within a string, I'm trying to update multiple instances of the same word with different values.
This is an overly simplified example, but given the following string:
"The first car I saw was color, the second car was color and the third car was color"
The first instance of the word color I want to replace with "red", the second instance should be "green" and the third instance should be "blue".
What I thought to try was a regex pattern to find boundried words, interate through a loop and replace them one at a time. See the example code below.
var colors = new List<string>{ "reg", "green", "blue" };
var sentence = "The first car I saw was color, the second car was color and the third car was color";
foreach(var color in colors)
{
var regex = new Regex("(\b[color]+\b)");
sentence = regex.Replace(sentence, color, 1);
}
However, the word "color" never gets replaced with the appropriate color name. I can't find what I've been doing wrong.