using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; using System.IO; public class Test { public static void Main() { string body = "hello [context.world]!! hello [context.anotherworld] and [context.text]"; Dictionary dyn = new Dictionary(){ {"world", "earth"}, {"anotherworld", "mars"} }; Console.WriteLine(Regex.Replace(body, @"\[context\.(\w+)]", m => dyn.ContainsKey(m.Groups[1].Value) ? dyn[m.Groups[1].Value] : m.Value)); } }