using System.Text.RegularExpressions;
public static string SafeReplace(string input, string find, string replace, bool matchWholeWord)
string textToFind = matchWholeWord ? string.Format(@"(?<=^|\s){0}(?=[$|\s])", Regex.Escape(find)) : Regex.Escape(find);
return Regex.Replace(input, textToFind, replace);
public static void Main()
Console.WriteLine(SafeReplace("Peter[='111222'] + APeter[='111222']", "Peter[='111222']", "@", true));
Console.WriteLine(SafeReplace("asd.len + hasd.len", "asd.len", "@", true));
Console.WriteLine(SafeReplace("Q5.count[=1] = 0 and hQ5.count[=1] > 0", "Q5.count[=1]", "@", true));