// There's some similar questions on SO, but none of them seem to cover both replacing a whole word (enclosed in spaces) and its first occurence. Using both at the same time is what is causing me problems.
I want to replace the first occurence of a word surrounded by spaces and I'm running into some problems.
I have a string in range.Text that contains a long string. I want to find a words alike "@val1" "@val2" etc. and replace them with values from my values list. Here is how I do that :
while(i < valueCount && range.Text.Contains("@val"))
{
for (int j = 0; j < valueLimit; j++)
{
string pattern = $@"\b@val{ j + 1 }\b";
Regex regex = new Regex(pattern, RegexOptions.IgnoreCase);
Match match = regex.Match(range.Text);
if (match.Success)
{
range.Text = regex.Replace(range.Text, values[j], 1);
i++;
}
}
}
Now the problem is that for some reason match.Success is never true, even though I'm pretty sure that there's plenty of values like those I search for in it.
// Example string -
"1\t@val1\r2\t@val2\r3\t@val3\r4\t@val4\r5\t@val5\r6\t@val6\r7\t@val7\r8\t@val8\r9\t@val9\r10\t@val10\r11\t@val11\r12\t@val12\r13\t@val13\r14\t@val14\r15\t@val15\r\r"
The \t s and \r s I expect to be ignored, but spaces are what is important to me. Otherwise I'll have @val110 replaced when loop is at @val11 or @val10. Two vals will never be separated with just a tab. They will always be enclosed in two spaces in the long string.