fork download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text.RegularExpressions;
  4. using System.Linq;
  5. public class Test
  6. {
  7. public static void Main()
  8. {
  9. var userDic = new Dictionary<string, string> {
  10. {"399969178745962506", "One"},
  11. {"104729417217032192", "Two"}
  12. };
  13. var p = @"<@!?(\d+)>";
  14. var s = "<@399969178745962506> hello to <@!104729417217032192>";
  15. Console.WriteLine(
  16. Regex.Replace(s, p, m => userDic.ContainsKey(m.Groups[1].Value) ?
  17. userDic[m.Groups[1].Value] : m.Value
  18. )
  19. );
  20. Console.WriteLine(
  21. Regex.Replace(s, @"(<@!?)(\d+)>", m => userDic.ContainsKey(m.Groups[2].Value) ?
  22. $"{m.Groups[1].Value}{userDic[m.Groups[2].Value]}>" : m.Value
  23. )
  24. );
  25. }
  26.  
  27.  
  28. }
  29.  
  30.  
Success #stdin #stdout 0.08s 20036KB
stdin
Standard input is empty
stdout
One hello to Two
<@One> hello to <@!Two>