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;
  (* ?Y果
  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;

相关阅读 >>

Delphi串口通信编程

Delphi fmx用timage显示不同格式的图片

Delphi的split函数 4个版本

Delphi窗体显示echarts图表

Delphi 实现卸载windows应用程序(类似360软件管家-卸载程序)

线程池的概念

Delphi hash md5

Delphi xe android 使用system.zip单元释放资源文件

Delphi 简单字符串截取函数

Delphi pascal 单元文件的特殊定义格式

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...