title: Unexpected RegEx behavior in Delphi XE
body: <p>Delphi XE, using Delphi's own RegularExpressions unit.</p>
  
  <p>I'm attempting to correct some bad RTF code, where 'bookmark' tags cross the boundaries of a table cell. Seems simple enough. The code I'm using is below. Here's the general idea.</p>
  
  <p>Given this text</p>
  
  <pre><code>{\*\bkmkstart BM0}\plain\f0\fs24\cf0 ^\cell}
  </code></pre>
  
  <p>Look for a match to this pattern (there should be exactly one in the given text):</p>
  
  <pre><code>{\\\*\\bkmkstart BM0}\\plain\\f[0-9]\\fs[0-9]+\\cf[0-9] \^\\cell}
  </code></pre>
  
  <p>When found, replace it with this (non-RegEx) string:</p>
  
  <pre><code>{\*\bkmkstart BM0}\plain\f0\fs24\cf0 ^{\*\bkmkend BM0}\plain\f0\fs24\cf0 \cell}
  </code></pre>
  
  <p>The expected results are that the first string should be replaced with the last string, eg:</p>
  
  <pre><code>{\*\bkmkstart BM0}\plain\f0\fs24\cf0 ^\cell} *becomes*
  {\*\bkmkstart BM0}\plain\f0\fs24\cf0 ^{\*\bkmkend BM0}\plain\f0\fs24\cf0 \cell}
  </code></pre>
  
  <p>However, the result I'm actually getting is this:</p>
  
  <pre><code>{\*\bkmkstart BM0}\plain{\*\bkmkstart bm0}\plain\f0\fs24\cf0 ^\cell}\fs24\cf0 ^{\*\bkmkend BM0}\plain{\*\bkmkstart bm0}\plain\f0\fs24\cf0 ^\cell}\fs24\cf0 \cell}
  </code></pre>
  
  <p>It looks as if the RegEx parser is getting horribly confused somehow, but I can't even characterize what is happening. It's not a mere double replacement, or an insertion instead of replacement. The 'ReplaceWith' string does seem to be the source of the confusion, though. If I use a nice simple 'XXXX' for the ReplaceWith string, instead of the RTF, it works exactly as it should.</p>
  
  <p>So, any ideas how/why the RegEx search/replace is breaking so strangely here?</p>
  
  <p>Here is the code I'm using:</p>
  
  <pre><code>procedure TfrmMain.btnProcessClick(Sender: TObject);
  const
    SourceString = '{\*\bkmkstart BM0}\plain\f0\fs24\cf0 ^\cell}';
    RegExFind = '{\\\*\\bkmkstart BM0}\\plain\\f[0-9]\\fs[0-9]+\\cf[0-9] \^\\cell}';
    ReplaceWith = '{\*\bkmkstart BM0}\plain\f0\fs24\cf0 ^{\*\bkmkend BM0}\plain\f0\fs24\cf0 \cell}';
  var
    ResultStr: string;
    MyRegEx: TRegEx;
  begin
    MyRegEx := TRegEx.Create (RegExFind);
    ResultStr := MyRegEx.Replace (SourceString, ReplaceWith);
    ShowMessage (ResultStr);
  end;
  </code></pre>