title: C# Replace regex matched pattern with a captured group
body: <p>I am trying to replace a pattern in my string with a captured group, but not directly. The value for the captured group resides in a dictionary, keyed by the captured group itself. How can I achieve this?</p>
  
  <p>This is what I'm trying:</p>
  
  <pre><code>string body = "hello [context.world]!! hello [context.anotherworld]";
  Dictionary&lt;string, string&gt; dyn = new Dictionary&lt;string, string&gt;(){ {"world", "earth"}, {"anotherworld", "mars"}};
  Console.WriteLine(Regex.Replace(body, @"\[context\.(\w+)\]", dyn["$1"]));
  </code></pre>
  
  <p>I keep getting KeyNotFoundException which indicates to me that the $1 is getting interpreted literally during dictionary lookup. </p>