How to Use Delphi TRegEx to replace a particular capture group?
<p>I am trying to use TRegex in Delphi XE7 to do a search and replace in a string.</p>

<p>The string looks like this "@FXXX(b, v," and I want to replace the second integer value v.
For example: 
  @F037(594,2027,-99,-99,0,0,0,0)</p>

<p>might become
  @F037(594,Fred,-99,-99,0,0,0,0)</p>

<p>I am a newbie at RegEx but made up this pattern that seems to work fine for finding the match and identifying the right capturing group for the "2027" (the part below in parentheses).  Here it is:</p>

<p>@F\d{3}(\s*\d{1,5}\s*,\s*(\d{1,5})\s*,</p>

<p>My problem is that I cannot work out how to replace just the captured group "2027" using the Delphi TRegEx implementation.  I am getting rather confused about TMatch and TGroup and how to use them.  Can anyone suggest some sample code?  I also suspect I am not understanding the concept of backreferences.</p>

<p>Here is what I have so far:</p>

<pre><code>Uses
   RegularExpressions;

//The function that does the actual replacement
function TForm6.DoReplace(const Match: TMatch): string;
begin
  //This causes the whole match to be replaced.
  //@F037(594,2027,-99,-99,0,0,0,0) becomes  Fred-99,-99,0,0,0,0)
  //How to just replace the first matched group (ie 2027)?
  If Match.Success then
     Result := 'Fred';
end;


//Code to set off the regex replacement based on source text in Edit1 and put the result back into Memo1
//Edit1.text set to  @F037(594,2027,-99,-99,0,0,0,0)

procedure TForm6.Button1Click(Sender: TObject);

var
  regex: TRegEx;
  Pattern: string;
  Evaluator: TMatchEvaluator;

begin
  Memo1.Clear;
  Pattern := '@F\d{3}\(\s*\d{1,5}\s*,\s*(\d{1,5})\s*,';
  RegEx.Create(Pattern);
  Evaluator := DoReplace;
  Memo1.Lines.Add(RegEx.Replace(Edit1.Text, Pattern, Evaluator));
end;
</code></pre>