using System.Text.RegularExpressions;
public static void Main()
string replace = @"$1DATA_SENSITIVE&";
string pattern = @"(&password=)(.*?)(&)";
var text = @"a=123&password=Xgen@123&code=456";
var replaced = text.TryReplaceRegEx(pattern, replace, RegexOptions.IgnoreCase);
Console.WriteLine(replaced);
public static class Extensions {
public static string TryReplaceRegEx(this string source, string pattern, string newValue, RegexOptions options)
if (string.IsNullOrEmpty(source) || string.IsNullOrEmpty(pattern))
return Regex.Replace(source, pattern, newValue, options);