title: Unescape all nested quotes
body: <p>I want to unescape all nested quotes within a string. The following examples are given as literal (C# or F#) style .NET strings, not surround by quotes:</p>
  
  <ul>
  <li><code>[(\"hello world\", 2); (\"goodbye\", 3)]</code> doesn't change</li>
  <li><code>[(\"hello\"world\", 2); (\"go\"o\"d\"bye\", 3)]</code> becomes <code>[(\"hello\\\"world\", 2); (\"go\\\"o\\\"d\\\"bye\", 3)]</code></li>
  </ul>
  
  <p>I'm not sure whether this can be done with <code>Regex(pattern, "\\\"").Replace</code> but I am still too much of a regex novice for the solution to come easily to me. Any solution, regex if possible, would be appreciated. </p>
  
  <p><em><strong>Edit</em></strong></p>
  
  <p>Thanks for feedback so far from everyone, I see now that since there is no distinguishing between opening and closing quotes, the syntax is ambiguous and the direction I was going can't be worked. So I will give the big picture in hopes of a some new direction.</p>
  
  <p>I am working on a project which converts F# Quotations into F# source code strings. So a I have function <code>source: Expr -&gt; string</code> which should produce a string which when printed to a typical console like FSI is valid F# code. For this problem, I am looking to improve the way <code>Value</code> quotation expressions are sprinted. Currently I do something like the following (see starting at line 312 of <a href="http://code.google.com/p/unquote/source/browse/trunk/Unquote/Sprint.fs" rel="nofollow">http://code.google.com/p/unquote/source/browse/trunk/Unquote/Sprint.fs</a> for real code):</p>
  
  <pre><code>match expr with
  | Value(o, _) -&gt;
    match o with
    | null -&gt; "null"
    | _ -&gt; sprintf "%A" o
  </code></pre>
  
  <p>But then, for example, I get the following</p>
  
  <pre><code>&gt; &lt;@ "\r\"\n" @&gt; |&gt; source |&gt; stdout.WriteLine;;
  "
  "
  "
  val it : unit = ()
  </code></pre>
  
  <p>instead of the desired</p>
  
  <pre><code>&gt; &lt;@ "\r\"\n" @&gt; |&gt; source |&gt; stdout.WriteLine;;
  "\r\"\n"
  val it : unit = ()
  </code></pre>
  
  <p>If I only needed to consider <code>Values</code> encapsulating strings, that would be easy with something like</p>
  
  <pre><code>let unescape s =
      ["\\","\\\\"
       "\b","\\b"
       "\n","\\n" 
       "\r","\\r" 
       "\t","\\t"
       "\"", "\\\""]
      |&gt; List.fold (fun (s:string) (before, after) -&gt; s.Replace(before, after)) s
  </code></pre>
  
  <p>The problem is, any object may be a <code>Value</code>, including those with structured formats used by <code>sprint "%A"</code> which I'd like to leverage as much as possible (so while I could go through and handle a finite set of cases like lists, arrays, tuples, and so forth, that isn't as general as I'd like it to be): so sprinting a <code>list&lt;string*int&gt;</code> <code>Value</code> needs special care for example, since we need to distinguish between quotes which should be displayed literally for string construction, versus those which should be displayed as escape sequences.</p>
  
  <p>Any ideas welcome, thanks!</p>