title: Implementing parser for escape sequences
body: <p>I'm want to parse a custom string format that is persisting an object graphs state.  This is ASP.NET scenario and I wanted something easy to use on the client (JavaScript) and server (C#).</p>
  
  <p>I have a format something like </p>
  
  <pre><code>{Name1|Value1|Value2|...|ValueN}{Name2|Value1|...}{...}{NameN|...}
  </code></pre>
  
  <p>In this format I have 3 delimiters, <code>{</code>, <code>}</code>, and <code>|</code>.  Further, because these characters are conceivable in the name/values, I defined an escape sequence using the very common <code>\</code>, such that <code>\{</code>, <code>\}</code> and <code>\|</code> are all interpreted as normal versions of themselves and of course <code>\\</code> is a backslash.  All pretty standard.</p>
  
  <p>Originally I tried to use a regex to try to parse out the string representation of an object with something like this <code>(?&lt;!\\)\{(.*?)(?&lt;!\\)\}</code>.  Keep in mind <code>\</code>, <code>{</code>, and <code>}</code> are all reserved in regexes.  This of course will be able to parse out something like <code>{category|foo\}|bar\{}</code> correctly.  However I realized it would fail with something like <code>{category|foo|bar\\}</code>. </p>
  
  <p>It only took me a minute or so to try this <code>(?&lt;!(?&lt;!\\)\\)\{(.*?)(?&lt;!(?&lt;!\\)\\)\}</code> and realize that this approach was not possible given that you'd need an infinite number of negative lookbehinds to deal with a potential infinite number of escape sequences.  Of course it's unlikely that I'd ever have more than one or two levels so I could probably hard code it.  However, I feel that this is a common enough problem it should have a well defined solution.</p>
  
  <p>My next approach was to try to write a defined parser where I actually scanned the input buffer and consumed each character in a forward only method.  I haven't actually finished this yet but it seems overly complicated and I feel I must be missing something obvious. I mean we've had parsers as long as we've had computer languages.</p>
  
  <p>So my question would be what is the simplest, efficient and elegant way to decode an input buffer like this with possible escape sequences? </p>