fork download
  1. using System;
  2. using System.Text.RegularExpressions;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5.  
  6. public class Test
  7. {
  8. private static string LookupReplace(string text, List<string> newList)
  9. {
  10. return "~" + newList.IndexOf(text).ToString() + "~";
  11. }
  12.  
  13. private static string NumberedReplace()
  14. {
  15. i++;
  16. return "~" + i.ToString() + "~";
  17. }
  18.  
  19. public static int i = -1;
  20.  
  21. public static void Main()
  22. {
  23. string text = "iif(instr(|Wellington, New Zealand|,|,|)>0,|Wellington, New Zealand|,|Wellington, New Zealand| & |, | & |New Zealand|) & | to | & iif(instr(|Jeddah, Saudi Arabia|,|,|)>0,|Jeddah, Saudi Arabia|,|Jeddah, Saudi Arabia| & |, | & |Saudi Arabia|) & iif(|Jeddah, Saudi Arabia|=||,||,| via | & |Jeddah, Saudi Arabia|)";
  24. var re = new Regex(@"\|.*?\|");
  25. var newList = re.Matches(text)
  26. .OfType<Match>()
  27. .Select(m => m.Value)
  28. .ToList();
  29. string result = re.Replace(text, x => LookupReplace(x.Value, newList));
  30. Console.WriteLine(result);
  31.  
  32. result = re.Replace(text, x => NumberedReplace());
  33. Console.WriteLine(result);
  34. }
  35.  
  36. }
Success #stdin #stdout 0.1s 24560KB
stdin
Standard input is empty
stdout
iif(instr(~0~,~1~)>0,~0~,~0~ & ~4~ & ~5~) & ~6~ & iif(instr(~7~,~1~)>0,~7~,~7~ & ~4~ & ~12~) & iif(~7~=~14~,~14~,~16~ & ~7~)
iif(instr(~0~,~1~)>0,~2~,~3~ & ~4~ & ~5~) & ~6~ & iif(instr(~7~,~8~)>0,~9~,~10~ & ~11~ & ~12~) & iif(~13~=~14~,~15~,~16~ & ~17~)