Search code examples
c#regexword-boundaryword-boundaries

Word boundaries not matching when the word starts or ends with special character like square brackets


I want to replace string which is a square bracket with another number. I am using regex replace method.

Sample input:

This is [test] version.

Required output (replacing "[test]" with 1.0):

This is 1.0 version.

Right now regex is not replacing the special character. Below is the code which I have tried:

 string input= "This is [test] version of application.";

 string stringtoFind = string.Format(@"\b{0}\b", "[test]");

 Console.WriteLine(Regex.Replace(input, stringtoFind, "1.0"));

There may be any special character in input and stringtoFind variables.


Solution

  • You must account for two things here:

    • Special characters must be escaped with a literal \ symbol that is best done using Regex.Escape method when you have dynamic literal text passed as a variable to regex
    • It is not possible to rely on word boundaries, \b, because the meaning of this construct depends on the immediate context.

    You can use dynamic adaptive word boundaries (see my YT video about these word boundaries):

    string input= "This is [test] version of application.";
    string key = "[test]";
    string stringtoFind = $@"(?!\B\w){Regex.Escape(key)}(?<!\w\B)";
    Console.WriteLine(Regex.Replace(input, stringtoFind, "1.0"));
    

    You may also use Regex.Escape with unambiguous word boundaries (?<!\w) and (?!\w):

    string input= "This is [test] version of application.";
    string key = "[test]";
    string stringtoFind = $@"(?<!\w){Regex.Escape(key)}(?!\w)";
    Console.WriteLine(Regex.Replace(input, stringtoFind, "1.0"));
    

    Note that if you want to replace a key string when it is enclosed with whitespaces use

    string stringtoFind = $@"(?<!\S){Regex.Escape(key)}(?!\S)";
                             ^^^^^^                    ^^^^^