TPerlRegExOnReplace (Delphi)

From RAD Studio Code Examples
Jump to: navigation, search

Description

This example uses the TPerlRegEx OnReplace event handler to handle the ReplaceAll event. Place a TButton and a TEdit on the form. Make PerlRegEx1 a global variable TPerlRegEx and instantiate it in FormCreate. Since TPerlRegEx is not a component, enter the declaration of PerlRegEx1Replace in the Form1 declaration and load the TPerlRegEx OnReplace method in FormCreate.

Code

procedure TForm1.Button1Click(Sender: TObject);
begin
  with PerlRegEx1 do begin
    RegEx := 'Embarcadero|Inprise|Microsoft';
    Subject := Edit1.Text;
    ReplaceAll;
    Edit1.Text := Subject;
  end;
end;

procedure TForm1.PerlRegEx1Replace(Sender: TObject; var ReplaceWith: UTF8String);
begin
  if PerlRegEx1.MatchedText = 'Inprise' then ReplaceWith := 'Embarcadero'
    else if PerlRegEx1.MatchedText = 'Embarcadero' then ReplaceWith := ':-)'

    else ReplaceWith := ':-('
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  PerlRegEx1:= TPerlRegEx.Create;
  PerlRegEx1.OnReplace:= PerlRegEx1Replace;
end;

Uses