fork download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text.RegularExpressions;
  4. public class Test
  5. {
  6. public static void Main()
  7. {
  8. var s = "(Price+Discounted_Price)*2-Max.Price";
  9. var dct = new Dictionary<string, string>();
  10. dct.Add("Price", "A1");
  11. dct.Add("Discounted_Price", "A2");
  12. dct.Add("Max.Price","A3");
  13. var listAbove = new List<string> { "Price", "Discounted_Price", "Max.Price" };
  14. var result = s;
  15. foreach (string phrase in listAbove)
  16. {
  17. result = Regex.Replace(result, @"\b(?<![\w.])" + Regex.Escape(phrase) + @"\b(?![\w.])", dct[phrase]);
  18. }
  19. Console.WriteLine(result);
  20. //var res = Regex.Replace(s, @"(?<![\w.])[\w.]+(?![\w.])",
  21. //x => dct.ContainsKey(x.Value) ? dct[x.Value] : x.Value);
  22. //Console.WriteLine(res);
  23. }
  24. }
Success #stdin #stdout 0.12s 24456KB
stdin
Standard input is empty
stdout
(A1+A2)*2-A3