See: http://codingdojo.org/kata/DictionaryReplacer/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
internal class Program
{
public static void Main(string[] args)
{
var input = "My name is $name$ $surname$ and I'm $age$ years old. $bio$";
var values = new Dictionary<string, string>() {
{"name", "John"},
{"surname", "Doe"},
{"age", "42"}
};
Console.WriteLine(DictionaryReplacer.Replace(input, values));
// Output: My name is John Doe and I'm 42 years old.
}
}
internal static class DictionaryReplacer
{
public static string Replace(string input, IDictionary<string, string> values)
{
return Regex.Replace(input, @"\$(\w+)\$", match => {
var key = match.Groups[1].Value;
return values.ContainsKey(key) ? values[key] : string.Empty;
});
}
}