using System; using System.Text.RegularExpressions; using System.Linq; public class Test { public static void Main() { string s1 = "
Hello world, hello world
"; 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("{1}", match++, m.Value), RegexOptions.IgnoreCase); Console.Write(s1); } }