0

// 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.

1 Answers1

2

The issue appears to be the leading \b in your pattern. With that in place, the match always fails.

The trailing one is essential so that @val1 doesn't also incorrectly match @val10, but I'm not seeing what the leading one is for, and it's causing the match to fail.

Try changing:

string pattern = $@"\b@val{ j + 1 }\b";

to

string pattern = $@"@val{ j + 1 }\b";

Other than that, the code seems to achieve what you describe.

Hope this helps

Trevor
  • 1,251
  • 1
  • 9
  • 11
  • 1
    [\b The match must occur on a boundary between a \w (alphanumeric) and a \W (nonalphanumeric) character.](https://learn.microsoft.com/en-us/dotnet/standard/base-types/regular-expression-language-quick-reference) - this means that "\b@" will fail because the @ is non-alphanumeric so the \b is not at the boundary between alphanumeric & non-alphanumeric. – PaulF Feb 20 '18 at 16:49
  • 1
    @PaulF Exactly what I was thinking. My question was more what OP was trying to achieve with it, although I didn't make that clear. I'll update the answer to incorporate that detail and clarify my question. Thanks – Trevor Feb 20 '18 at 16:57