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 了.

  • 相关阅读:
    nginx能访问html静态文件但无法访问php文件
    LeetCode "498. Diagonal Traverse"
    LeetCode "Teemo Attacking"
    LeetCode "501. Find Mode in Binary Search Tree"
    LeetCode "483. Smallest Good Base" !!
    LeetCode "467. Unique Substrings in Wraparound String" !!
    LeetCode "437. Path Sum III"
    LeetCode "454. 4Sum II"
    LeetCode "445. Add Two Numbers II"
    LeetCode "486. Predict the Winner" !!
  • 原文地址:https://www.cnblogs.com/del/p/1641084.html
Copyright © 2011-2022 走看看