Delphi XE using Delphi's own regular expression block.
I am trying to fix some bad RTF code where the bookmark tags intersect the borders of a table cell. Seems simple enough. The code I'm using is below. Here is a general idea.
Given this text
{\*\bkmkstart BM0}\plain\f0\fs24\cf0 ^\cell}
Find a match for this pattern (there should be exactly one in this text):
{\\\*\\bkmkstart BM0}\\plain\\f[0-9]\\fs[0-9]+\\cf[0-9] \^\\cell}
If it is found, replace it with this line (non-RegEx):
{\*\bkmkstart BM0}\plain\f0\fs24\cf0 ^{\*\bkmkend BM0}\plain\f0\fs24\cf0 \cell}
Expected results: the first line should be replaced by the last line, for example:
{\*\bkmkstart BM0}\plain\f0\fs24\cf0 ^\cell} *becomes*
{\*\bkmkstart BM0}\plain\f0\fs24\cf0 ^{\*\bkmkend BM0}\plain\f0\fs24\cf0 \cell}
However, the result that I really get is this:
{\*\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}
, RegEx - , . , . 'ReplaceWith' . "XXXX" ReplaceWith, RTF, , .
, , / / RegEx ?
, :
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;