using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text.RegularExpressions; public class Test { private const string RegExpReplacement = "$1***$2"; private static readonly string[] PasswordLiterals = { "password", "RootPassword", "PassPhrase" }; public static string Replace2(string sensitiveInfo) { var multiplePattern = $"(\"(?:{string.Join("|", PasswordLiterals)})\":\")[^\"]*(\")"; return Regex.Replace(sensitiveInfo, multiplePattern, RegExpReplacement, RegexOptions.IgnoreCase); } public static void Main() { Console.Write(Replace2("\"Password\":\"123\",\"RootPassword\":\"123qwe\",\"PassPhrase\":\"phrase\"")); } }