fork(4) 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. public static void Main()
  10. {
  11. var dct = new Dictionary<string, string>();
  12. dct.Add("bla", "boo");
  13. dct.Add("bla test", "ZZZ");
  14. var pat = $@"\b(https://\S+|www\.\S+)|(?:{string.Join("|",dct.Keys.Select(k => Regex.Escape(k)).OrderByDescending(x => x.Length))})";
  15. Console.WriteLine(pat);
  16. var input ="bla bla test test1 test3. Go to www.something.com/bla/something";
  17. var output = Regex.Replace(input, pat, m => m.Groups[1].Success ? m.Groups[1].Value : dct[m.Value]);
  18. Console.Write(output);
  19.  
  20. //var output = "boo boo test test1 test3...";
  21. }
  22. }
Success #stdin #stdout 0.04s 134720KB
stdin
Standard input is empty
stdout
\b(https://\S+|www\.\S+)|(?:bla\ test|bla)
boo ZZZ test1 test3. Go to www.something.com/bla/something