TPerlRegExStoreGroups (Delphi)

From RAD Studio Code Examples
Jump to: navigation, search

Description

This example uses the TPerlRegEx StoreGroups method.

Code

procedure TForm1.Button1Click(Sender: TObject);
var S : UTF8String;
begin
  // The first example generates an ERegularExpressionsError with message 
  // 'Successful match required.' because the Groups have been cleared when the 
  // Subject is set to empty:

  S := 'The sheriff told us that Joe did it.';
  with PerlRegEx1 do begin
    RegEx := '(Joe|Jack|William|Avarell) did it';
    Subject := S;
    if Match then begin
      Subject := ''; // Save memory
      if GroupCount = 1 then
        S := 'Wanted: ' + Groups[1]
      else
        S := 'Nobody wanted';

      ShowMessage(S);
    end;
  end;
end;

procedure TForm1.Button2Click(Sender: TObject);
var S : UTF8String;
begin
  // The second example displays "Wanted: Joe".

  S := 'The sheriff told us that Joe did it.';
  with PerlRegEx1 do begin
    RegEx := '(Joe|Jack|William|Avarell) did it';
    Subject := S;
    if Match then begin
      StoreGroups;
      Subject := ''; // Save memory
      if GroupCount = 1 then
        S := 'Wanted: ' + Groups[1]

      else
        S := 'Nobody wanted';
      ShowMessage(S);
    end;
  end;
end;

Uses