title: Escape quotes for interpolated string
body: <p>I have a program that generates C# from bits of C# stored in an XML file. If I have a snippet like:</p>
  
  <pre><code>foo {bar}
  </code></pre>
  
  <p>I need to transform that into an interpolated string, like this:</p>
  
  <pre><code>$@"foo {bar}"
  </code></pre>
  
  <p>The problem is that, if I have quotes outside a placeholder, e.g.:</p>
  
  <pre><code>"foo" {bar}
  </code></pre>
  
  <p>I need to double those:</p>
  
  <pre><code>$@"""foo"" {bar}"
  </code></pre>
  
  <p>but ignore quotes inside placeholders:</p>
  
  <pre><code>foo {"bar"}
  </code></pre>
  
  <p>should produce:</p>
  
  <pre><code>$@"foo {"bar"}"
  </code></pre>
  
  <p>Also, need to look out for doubled braces:</p>
  
  <pre><code>foo {{"bar"}}
  </code></pre>
  
  <p>should produce:</p>
  
  <pre><code>$@"foo {{""bar""}}"
  </code></pre>
  
  <p>And perhaps the trickiest of all, if the placeholder is preceded and/or followed by an even number of braces:</p>
  
  <pre><code>foo {{{"bar"}}}
  </code></pre>
  
  <p>should produce:</p>
  
  <pre><code>$@"foo {{{"bar"}}}"
  </code></pre>
  
  <p>In short, if there's a placeholder then ignore everything inside. For the rest of the text, double quotes.</p>
  
  <p>Can this be accomplished using regular expressions? If not, what alternatives do I have?</p>