static string ParameterizeText(string input, IDictionary<string, string> parameters)
{
if (parameters == null || parameters.Keys.Count == 0) return input;
return ParameterPattern.Replace(input, match =>
{
int lCount = match.Groups[1].Value.Length,
rCount = match.Groups[3].Value.Length;
if ((lCount % 2) != (rCount % 2)) throw new InvalidOperationException("Unbalanced braces");
string lBrace = lCount == 1 ? "" : new string('{', lCount / 2),
rBrace = rCount == 1 ? "" : new string('}', rCount / 2);
string key = match.Groups[2].Value, value;
if (lCount % 2 == 0)
{
value = key;
}
else
{
if (!parameters.TryGetValue(key, out value))
{
return string.Empty;
}
}
return lBrace + value + rBrace;
});
}