zoukankan      html  css  js  c++  java
  • RegularExpressions(4) RegularExpressions 成员(一)


    主要成员有: IRegex、ICapture、IMatch、IMatchCollection、IGroup、IGroupCollection

    先看: ICapture; 常用的 IMatch、IGroup 都是从它继承而来; 作为一个底层接口一般不会被直接使用.
    它为 IMatch、IGroup 提供了三个属性: Index、Length、Value; 还有一个 ToString 方法也是获取 Value.

    IMatchCollection、IGroupCollection 分别是 IMatch、IGroup 的集合.
    作为集合都有 Count 属性和 Items[] 属性; 它们的 GetEnumerator 方法支持了 for in 循环.
    和线程支持相关的三个属性: IsReadOnly、IsSynchronized、SyncRoot 在当前版本并没有实现.

    另外 IGroupCollection 比 IMatchCollection 多出来一个 ItemsByName[] 属性, 用于获取指定名称的子表达式, 如:
    uses RegularExpressions;
    
    procedure TForm1.FormCreate(Sender: TObject);
    var
      Regex: IRegex;
      Match: IMatch;
      w,n: string;
    begin
      Regex := TRegex.Create('(?<Name1>[A-Za-z]+)(?<Name2>\d+)');
      Match := Regex.Match('AAA1 BBB2 AA11 BB22 A111 B222 AAAA');
    
      while Match.Success do
      begin
        w := Match.Groups.ItemsByName['Name1'].Value; { AAA ...}
        n := Match.Groups.ItemsByName['Name2'].Value; {1 ...}
        ShowMessageFmt('%s, %s', [w, n]);
        Match := Match.NextMatch;
      end;
    end;
    

    IMatchCollection 在当前版本应该尽量少用, 因为有个 bug:
    获取 IMatchCollection 后, 其中的 IMatch 对象不包含子表达式的信息!
    假如不需要子表达式的信息, 则可以使用(假如需要可以使用 IMatch.NextMatch 方法, 前面有例子):
    uses RegularExpressions;
    
    procedure TForm1.FormCreate(Sender: TObject);
    var
      Regex: IRegex;
      Input, Pattern: string;
      MatchCollection: IMatchCollection;
      Match: IMatch;
    begin
      Pattern := '([A-Za-z]+)(\d+)';
      Input := 'AAA1 BBB2 AA11 BB22 A111 B222 AAAA';
    
      Regex := TRegex.Create(Pattern);
    
      MatchCollection := Regex.Matches(Input);
      for Match in MatchCollection do
      begin
        ShowMessage(Match.Value);
      end;
    end;
    

    IMatch 与 IGroup 是两个重要的对象接口.

    IMatch 是表达式匹配的结果;
    其 Success 方法表示匹配是否成功;
    其 NextMatch 方法是继续匹配下一个, 并返回下一个 IMatch 对象.

    IGroup 表示一个子表达式的匹配结果, 一个 IMatch 中可能会包含若干个 IGroup.

    下面程序可遍历出所有匹配到的子表达式:
    uses RegularExpressions;
    
    procedure TForm1.FormCreate(Sender: TObject);
    var
      Regex: IRegex;
      Input, Pattern, str: string;
      Match: IMatch;
      Group: IGroup;
    begin
      Pattern := '([A-Za-z]+)(\d+)';
      Input := 'AAA1 BBB2 AA11 BB22 A111 B222 AAAA';
    
      Regex := TRegex.Create(Pattern);
      Match := Regex.Match(Input);
      
      while Match.Success do
      begin
        for Group in Match.Groups do
        begin
          Memo1.Lines.Add(Group.Value);
        end;
        Match := Match.NextMatch;
      end;
    end;
    

    说没了, 只剩 IRegex 了.

  • 相关阅读:
    【http】HTTP请求方法 之 OPTIONS
    【javascript基础】函数前面的一元操作符
    【javascript基础】运算符优先级
    【移动互联网开发】Zepto 使用中的一些注意点 【转】
    【jQuery】IE9 jQuery 1.9.1 报 Syntax error,unrecognized expression 错误
    一月收集几个有用的谷歌Chrome插件
    【Sizzle学习】之关于【初探 jQuery 的 Sizzle 选择器】这篇文章里的小bug
    【第三方类库】underscore.js源码---each forEach 每次迭代跟{}比较的疑惑
    vue-cli脚手架npm相关文件解读(2)webpack.prod.conf.js
    vue-cli脚手架npm相关文件解读(1)webpack.base.conf.js
  • 原文地址:https://www.cnblogs.com/del/p/1641084.html
Copyright © 2011-2022 走看看