在Delphi中使用PerlRegex查找字符串中的电子邮件

时间:2016-01-28 15:46:06

标签: regex perl delphi

我在Delphi中使用带有PerlRegex组件的正则表达式,我有一个正则表达式perl perl工作完美,但在Delphi中使用PerlRegex组件时却找不到任何东西

代码:

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
  System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, PerlRegex;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
  code: string;
  regex: TPerlRegEx;
begin
  code := 'sdaasd saassd test@hotmail.com sdasdsd test2@gmail.com sdsadsd asdasdasd';

  regex := TPerlRegEx.Create();

  regex.regex := 'qr/[A-Z0-9._%+-]+\@[A-Z0-9.-]+\.[A-Z]{2,4}/i';
  regex.Subject := code;

  while regex.MatchAgain do
  begin
    ShowMessage(regex.Groups[1]);
  end;
end;

end.

我可以使用PerlRegex恢复邮件吗?

1 个答案:

答案 0 :(得分:1)

qr/.../i是Perl 语言的一部分,而不仅仅是正则表达式语法。所以这一行

regex.regex := 'qr/[A-Z0-9._%+-]+\@[A-Z0-9.-]+\.[A-Z]{2,4}/i';

错了,应该是

regex.RegEx   := '[A-Z0-9._%+-]+\@[A-Z0-9.-]+\.[A-Z]{2,4}';
regex.Options := [preCaseLess];