'Find each instance and replace with unique value in string
I have some text in a string as below:
{121:SOMETHING1}}{4:
.
.
.
{121:SOMETHING2}}{4:
.
.
.
{121:SOMETHING3}}{4:
I want to sequentially find and replace the value between 121: and the first }. I can successfully do this using the following code in C#
var rx = @"121:(?<value>[\s\S]+?)}";
string temp = tag121;
string stringToChange = //as above
for (var m = Regex.Match(stringToChange , rx); m.Success; m = m.NextMatch())
{
temp = generateUniqueValue()
stringToChange= Regex.Replace(stringToChange, m.Value, temp);
}
However, if instead of having different values between 121: and } I have exactly the same value for all the tags e.g instead of SOMETHING1, SOMETHING 2 etc, I just have SOMETHING for all the lines, then this code does not work. It ends up setting just one value for all the lines instead of unique values for each.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
