using System;
using System.Text.RegularExpressions;
using System.Linq;
public class Test
{
public static void Main()
{
string s1 = "<p>Hello world, hello world</p>";
string[] terms = new string[] {"hello", "world"};
var match = 1;
s1 = Regex.Replace(s1,
String.Join("|", String.Join("|", terms.OrderByDescending(s => s.Length)
.Select(Regex.Escape))),
m => String.Format("<span id=\"m_{0}\">{1}</span>", match++, m.Value),
RegexOptions.IgnoreCase);
Console.Write(s1);
}
}
dXNpbmcgU3lzdGVtOwp1c2luZyBTeXN0ZW0uVGV4dC5SZWd1bGFyRXhwcmVzc2lvbnM7CnVzaW5nIFN5c3RlbS5MaW5xOwpwdWJsaWMgY2xhc3MgVGVzdAp7CglwdWJsaWMgc3RhdGljIHZvaWQgTWFpbigpCgl7CgkJc3RyaW5nIHMxID0gIjxwPkhlbGxvIHdvcmxkLCBoZWxsbyB3b3JsZDwvcD4iOwoJCXN0cmluZ1tdIHRlcm1zID0gbmV3IHN0cmluZ1tdIHsiaGVsbG8iLCAid29ybGQifTsKCQl2YXIgbWF0Y2ggPSAxOwoJCXMxID0gUmVnZXguUmVwbGFjZShzMSwKCQkgICAgU3RyaW5nLkpvaW4oInwiLCBTdHJpbmcuSm9pbigifCIsIHRlcm1zLk9yZGVyQnlEZXNjZW5kaW5nKHMgPT4gcy5MZW5ndGgpCgkJICAgICAgICAuU2VsZWN0KFJlZ2V4LkVzY2FwZSkpKSwKCQkgICAgbSA9PiBTdHJpbmcuRm9ybWF0KCI8c3BhbiBpZD1cIm1fezB9XCI+ezF9PC9zcGFuPiIsIG1hdGNoKyssIG0uVmFsdWUpLAoJCSAgICBSZWdleE9wdGlvbnMuSWdub3JlQ2FzZSk7CgkJQ29uc29sZS5Xcml0ZShzMSk7Cgl9Cn0=