TRegExpr Examples

uses RegExpr;
 
procedure TForm1.Button1Click(Sender: TObject);
// Validates the email address in Edit1
begin
  // Warning: this code should not be used to perform actual
  // email validation. You should check the RFC specification.
  // This is just a simplification to show the use of ExecRegExpr.
  if not ExecRegExpr('[\w\d\-\.]+@[\w\d\-]+(\.[\w\d\-]+)+',
      Edit1.Text) then begin
    ShowMessage('The email address is not valid');
    Edit1.SetFocus;
  end else
    ShowMessage('The email address is valid');
end;
 
procedure TForm1.Button2Click(Sender: TObject);
// Extracts email addresses contained in Memo1
var
  RegExpr: TRegExpr;
begin
  // Warning: this code will not extract all valid email addresses.
  // This is just a simplification to show the use of Exec and Match.
  ListBox1.Clear;
  RegExpr := nil;
  try
    RegExpr := TRegExpr.Create;
    if RegExpr <> nil then begin
      RegExpr.Expression := '[^\w\d\-\.]([\w\d\-\.]+@[\w\d\-]+'
                          + '(\.[\w\d\-]+)+)[^\w\d\-\.]';
      if RegExpr.Exec(Memo1.Text) then
        repeat
          ListBox1.Items.Add(RegExpr.Match[1]);
        until not RegExpr.ExecNext;
    end;
  except
  end;
  RegExpr.Free;
end;

同步内容