Delphi XE中将正则表达式TRegEx的使用

研究了如何在Delphi XE TRegEx中使用正则表达式。
 
RegularExpressions.TRegEx记录
TRegEx是一个开源PCRE库包装器。
 
使用并使用RegularExpressions单元(Delphi XE2之后的System.RegularExpressions单元)。
 
uses RegularExpressions;
由于TRegEx是记录类型,因此不需要Free。
 
IsMatch方法 输入字符串中是否有匹配项
使用类方法的示例
 
if TRegEx.IsMatch('Delphi', '[A-Z]+') then
  ShowMessage('Match');
使用实例方法的示例
 
var
  regex: TRegEx;
begin
  regex := TRegEx.Create('[A-Z]+');
  if regex.IsMatch('Delphi') then
    ShowMessage('Match');
Matche方法 获取与正则表达式匹配的第一个字符串
Matche方法是TMatch记录返回。
 
使用类方法的示例
 
var
  match: TMatch;
begin
  match := TRegEx.Match('Delphi XE', '[A-Z]+');
  ShowMessage(
    Format('%s (%d, %d)',
      [match.Value, match.Index, match.Length])); //=> D (1, 1)
使用实例方法的示例
 
var
  regex: TRegEx;
  match: TMatch;
begin
  regex := TRegEx.Create('[A-Z]+');
  match := regex.Match('Delphi XE');
  ShowMessage(
    Format('%s (%d, %d)',
      [match.Value, match.Index, match.Length])); //=> D (1, 1)
您可以使用TMatch的NextMatch方法获取下一个匹配的字符串。
 
var
  regex: TRegEx;
  match: TMatch;
begin
  regex := TRegEx.Create('[A-Z]+');
  match := regex.Match('Delphi XE');
  ShowMessage(
    Format('%s (%d, %d)',
      [match.Value, match.Index, match.Length])); //=> D (1, 1)
 
 
  match := match.NextMatch;
  ShowMessage(
    Format('%s (%d, %d)',
      [match.Value, match.Index, match.Length])); //=> XE (8, 2)
您可以检查它是否与TMatch的Success属性匹配。
 
var
  regex: TRegEx;
  match: TMatch;
begin
  regex := TRegEx.Create('[A-Z]+');
  match := regex.Match('Delphi XE');
  while match.Success do
  begin
    ShowMessage(match.Value);
    match := match.NextMatch();
  end;
可以使用TMatch的Groups属性获取与分组的正则表达式匹配的字符串。
 
var
  match: TMatch;
  group: TGroup;
begin
  match := TRegEx.Match('2012/5/14', '(\d+)/(\d+)/(\d)');
  ShowMessage(IntToStr(match.Groups.Count)); //=> 4
 
  for group in match.Groups do
  begin
    ShowMessage(Format('%s (%d, %d)',
      [group.Value, group.Index, group.Length]));
  end;
  (* 結果
  2012/5/1 (1, 8)
  2012 (1, 4)
  5 (6, 1)
  1 (8, 1)
  *)
Matches方法 获取与正则表达式匹配的所有字符串
匹配方法是,TMatchCollection返回该记录。
 
var
  match: TMatch;
  matches: TMatchCollection;
begin
  matches := TRegEx.Matches('Delphi XE', '[A-Z]+');
  ShowMessage(IntToStr(matches.Count));
 
  match := matches.Item[0];
  ShowMessage(
    Format('%s (%d, %d)',
      [match.Value, match.Index, match.Length])); //=> D (1, 1)
 
  match := matches.Item[1];
  ShowMessage(
    Format('%s (%d, %d)',
      [match.Value, match.Index, match.Length])); //=> XE (8, 2)
Matches方法支持in do。
 
var
  regex: TRegEx;
  match: TMatch;
  matches: TMatchCollection;
begin
  regex := TRegEx.Create('[A-Z]+');
  matches := regex.Matches('Delphi XE');
  ShowMessage(IntToStr(matches.Count)); 
 
  for match in matches do
    ShowMessage(
      Format('%s (%d, %d)',
        [match.Value, match.Index, match.Length]));
Replace方法替换 与正则表达式匹配的字符串
//替换
s := TRegEx.Replace('Delphi XE', '[A-Z]', '_');
ShowMessage(s); //=> _elphi __
通过将函数作为参数传递,可以根据匹配的字符指定要替换的字符。
 
(* マッチした数値に1を加える *)
function TForm1.IncNum(const Match: TMatch): string;
begin
  Result := IntToStr(StrToInt(Match.Value) + 1);
end;
 
//替换
var
  s: UnicodeString;
begin
  s := TRegEx.Replace('Delphi 2009', '\d+', IncNum);
  ShowMessage(s); //=> Delphi 2010
拆分方法 拆分字符串
var
  splits: TArray<string>;
  s: string;
begin
  splits := TRegEx.Split('Delphi C++Builder/RadPHP', '[ /]');
  for s in splits do
    Memo1.Lines.Add(s);
转义方法转义 特殊字符
var
  s: string;
begin
  s := TRegEx.Escape('C++');
  ShowMessage(s); //=> C\+\+
验证正则表达式语法
如果在构造函数参数中指定了roCompiled,则将编译正则表达式。
如果正则表达式有问题,则引发异常。
 
try
  TRegEx.Create(Edit1.Text, [roCompiled]);
except on E: Exception do
  ShowMessage(E.Message);
end;
posted @ 2021-01-29 11:44  dqi1999  阅读(1586)  评论(0)    收藏  举报