RTL 中的正则表达式

版本新特性 / Delphi XE

// *** before: ***
// the library had no regular expressions at all. Either the check was
// written by hand with Pos and Copy, or a third-party unit had to be
// installed into the IDE
function LooksLikeEmail(const S: string): Boolean;
var
  P: Integer;
begin
  P := Pos('@', S);
  Result := (P > 1) and (Pos('.', Copy(S, P + 1, MaxInt)) > 0);
end;

// *** in version XE: ***
uses
  RegularExpressions;

var
  M: TMatch;
begin
  if TRegEx.IsMatch('user@example.com', '^\w+@\w+\.\w{2,}$') then
    Writeln('looks like an address');

  M := TRegEx.Match('order 1234 of 2010-05-06', '(\d{4})-(\d{2})-(\d{2})');
  if M.Success then
  begin
    Writeln(M.Value);              // 2010-05-06
    Writeln(M.Groups[1].Value);    // 2010
    Writeln(M.Groups[3].Value);    // 06
  end;
end;