fork download
  1. using System;
  2. using System.Text.RegularExpressions;
  3. using System.Linq;
  4. public class Test
  5. {
  6. public static void Main()
  7. {
  8. string s1 = "<p>Hello world, hello world</p>";
  9. string[] terms = new string[] {"hello", "world"};
  10. var match = 1;
  11. s1 = Regex.Replace(s1,
  12. String.Join("|", String.Join("|", terms.OrderByDescending(s => s.Length)
  13. .Select(Regex.Escape))),
  14. m => String.Format("<span id=\"m_{0}\">{1}</span>", match++, m.Value),
  15. RegexOptions.IgnoreCase);
  16. Console.Write(s1);
  17. }
  18. }
Success #stdin #stdout 0.04s 134592KB
stdin
Standard input is empty
stdout
<p><span id="m_1">Hello</span> <span id="m_2">world</span>, <span id="m_3">hello</span> <span id="m_4">world</span></p>