打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
RegularExpressions(5) RegularExpressions 成员(二...

//IRegex 的属性与方法 IRegex.GetGroupNames;         { 子表达式编号数组, 譬如有两个子表达式, 会得到 0,1,2; 这基本无用 }IRegex.GetGroupNumbers;       { 同上, 只是获取的是整数数组 }IRegex.GroupNameFromNumber(); { 应该是从子表达式编号获取子表达式的名称; 但没有实现, 来回都是编号 }IRegex.GroupNumberFromName(); { 同上一个是反着的; 基本都无用 }IRegex.IsMatch();             { 判断是否有所匹配; 如果只想知道是否匹配到, 用它应该最快 }IRegex.Match();               { 获取一个 IMatch 对象, 这是第一个匹配结果 }IRegex.Matches();             { 获取一个 IMatchCollection 对象; 这是匹配到的 IMatch 的集合 }IRegex.Replace();             { 替换 }IRegex.Split();               { 根据表达式分割字符串; 当前版本没有实现好, 暂时无用 }IRegex.ToString;              { 获取表达式文本 }IRegex.Save();                { 把表达式保存到流 }IRegex.Load();                { 从流中取回由 Save 保存的表达式 }IRegex.Options;               { 选项集合, 是只读的; 要设置只能从 Create 方法中 }{这其中需要重新看看的只有 Replace}//IRegex 是通过 TRegex 实现的, TRegex 还有下面几个静态类方法:TRegex.Escape();    { 编码需要转义的字符 }TRegex.Unescape();  { 还原 Escape 的编码 }TRegex.IsMatch();   { 同 IRegex.IsMatch }TRegex.Match();     { 同 IRegex.Match }TRegex.Matches();   { 同 IRegex.Matches }TRegex.Replace();   { 同 IRegex.Replace }TRegex.Split();     { 同 IRegex.Split }{ 使用这些个类方法会让代码更简单 }

下面是使用类方法简化程序的例子:
uses RegularExpressions;procedure TForm1.FormCreate(Sender: TObject);var  Match: IMatch;  MatchCollection: IMatchCollection;  Pattern, Input, str: string;begin  Pattern := '([A-Za-z]+)(\d+)';  Input := 'AAA1 BBB2 AA11 BB22 A111 B222 AAAA'//  Match := TRegex.Match(Input, Pattern);  ShowMessage(Match.Value); { AAA1 }  //  MatchCollection := TRegex.Matches(Input, Pattern);  ShowMessage(IntToStr(MatchCollection.Count)); { 4 }    //  str := TRegex.Replace(Input, Pattern, '◆');  ShowMessage(str); { ◆ ◆ ◆ ◆ ◆ ◆ AAAA }end;

Escape 与 Unescape 测试:
uses RegularExpressions;procedure TForm1.FormCreate(Sender: TObject);var  str: string;begin  str := TRegex.Escape('.$^{[(|)*+?\');  ShowMessage(str);  (* \.\$\^\{\[\(\|\)\*\+\?\\ *)  str := TRegex.Unescape(str);  ShowMessage(str);  (* .$^{[(|)*+?\ *)end;

Replace 函数有很多重载, 如:
uses RegularExpressions;procedure TForm1.FormCreate(Sender: TObject);var  Regex: IRegex;  Match: IMatch;  Pattern, Input, str: string;begin  Pattern := '([A-Za-z]+)(\d+)';  Input := 'AAA1 BBB2 AA11 BB22 A111 B222 AAAA';  Regex := TRegex.Create(Pattern);  { 替换全部 }  str := Regex.Replace(Input, '◆');   ShowMessage(str); { ◆ ◆ ◆ ◆ ◆ ◆ AAAA }  { 只替换前两个匹配 }  str := Regex.Replace(Input, '◆', 2);  ShowMessage(str); { ◆ ◆ AA11 BB22 A111 B222 AAAA }   { 从第 10 个字符开始, 只替换两个匹配 }  str := Regex.Replace(Input, '◆', 2, 10);  ShowMessage(str); { AAA1 BBB2 ◆ ◆ A111 B222 AAAA } end;

Replace 函数中还应该有一个 TMatchEvaluator 事件参数, 它可以执行一些更高级的替换, 遗憾的是当前版本也没有实现.

尽管有不少东西还没实现, 但试用下来感觉还是蛮不错的, 可以放弃 PerlRegEx 了!
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
DelPhi2007 中 使用Indy 的TCP连接教程
Delphi正则表达式使用方法(TPerlRegEx)
Delphi2007 unicode
谈谈 Delphi 的类型与指针[2]
delphi fileclose和closefile的区别
python学习笔记(一)
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服