// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; using System.Collections.Generic; using System.Text.RegularExpressions; namespace E { public class Program { public static string Replace(string input, Dictionary replacements) { string res = input; Regex re = new Regex(@"{(\w+)}"); foreach(Match m in re.Matches(input)) { string id = m.Groups[1].Value; res = res.Replace("{" + id + "}", replacements.ContainsKey(id) ? replacements[id] : ""); } return res; } public static void Main(string[] args) { string input = "A: {A} B: {B} C: {C} D: {D}"; Dictionary replacements = new Dictionary(); replacements.Add("A", "This is A"); replacements.Add("C", "This is C"); string output = Replace(input, replacements); Console.WriteLine(input); Console.WriteLine(output); } } }