fork download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text.RegularExpressions;
  6.  
  7. public class Test
  8. {
  9. private const string RegExpReplacement = "$1***$2";
  10.  
  11. private static readonly string[] PasswordLiterals =
  12. {
  13. "password",
  14. "RootPassword",
  15. "PassPhrase"
  16. };
  17.  
  18. public static string Replace2(string sensitiveInfo)
  19. {
  20. var multiplePattern = $"(\"(?:{string.Join("|", PasswordLiterals)})\":\")[^\"]*(\")";
  21. return Regex.Replace(sensitiveInfo, multiplePattern, RegExpReplacement, RegexOptions.IgnoreCase);
  22. }
  23.  
  24. public static void Main()
  25. {
  26. Console.Write(Replace2("\"Password\":\"123\",\"RootPassword\":\"123qwe\",\"PassPhrase\":\"phrase\""));
  27.  
  28. }
  29. }
Success #stdin #stdout 0.08s 21092KB
stdin
Standard input is empty
stdout
"Password":"***","RootPassword":"***","PassPhrase":"***"