zoukankan      html  css  js  c++  java
  • Delphi 正则表达式

    在Delphi中使用正则表达式,我以前用的是PaxScripter里面的——TRegExpr, 引用单元:RegExpr1。但有很多人使用的是RegExpr 是http://RegExpStudio.com   提供的,现在好像免费了,而且包含在Lazarus里面。 Freepascal本身还提供了Regex单元和TRegexEngine类用以解析正则表达式。

    Delphi自2009版开始提供了原生态的正则表达式支持,而后XE版好像有有所调整,但都是通过应用RegularExpressions单元,但与以前第三方方法有区别的是,它的实现是通过Record结构体而不是Class类(万一博客演示2009版,也是Class ——TRegex,并配合IRegex接口 ),据说是为了效率。

    主要功能实现是通过使用封装后的类TRegEx,对于所有功能实现它定义了重载后的方法和静态类方法,如下:

     
      TMatchEvaluator = function(const Match: TMatch): string of object;
    
      TRegEx = record
      private
        FOptions: TRegExOptions;
        FMatchEvaluator: TMatchEvaluator;
        FNotifier: IInterface;
        FRegEx: TPerlRegEx;
        procedure InternalOnReplace(Sender: TObject; var ReplaceWith: UTF8String);
      public
        constructor Create(const Pattern: string); overload;
        constructor Create(const Pattern: string; Options: TRegExOptions); overload;
    
        function IsMatch(const Input: string): Boolean; overload;
        function IsMatch(const Input: string; StartPos: Integer): Boolean; overload;
        class function IsMatch(const Input, Pattern: string): Boolean;overload; static;
        class function IsMatch(const Input, Pattern: string; Options: TRegExOptions): Boolean; overload; static;
    
        class function Escape(const Str: string; UseWildCards: Boolean = False): string; static;
    
        function Match(const Input: string): TMatch; overload;
        function Match(const Input: string; StartPos: Integer): TMatch; overload;
        function Match(const Input: string; StartPos, Length: Integer): TMatch; overload;
        class function Match(const Input, Pattern: string): TMatch; overload; static;
        class function Match(const Input, Pattern: string; Options: TRegExOptions): TMatch; overload; static;
    
        function Matches(const Input: string): TMatchCollection; overload;
        function Matches(const Input: string; StartPos: Integer): TMatchCollection; overload;
        class function Matches(const Input, Pattern: string): TMatchCollection; overload; static;
        class function Matches(const Input, Pattern: string; Options: TRegExOptions): TMatchCollection; overload; static;
    
        function Replace(const Input, Replacement: string): string; overload;
        function Replace(const Input: string; Evaluator: TMatchEvaluator): string; overload;
        function Replace(const Input, Replacement: string; Count: Integer): string; overload;
        function Replace(const Input: string; Evaluator: TMatchEvaluator; Count: Integer): string; overload;
        class function Replace(const Input, Pattern, Replacement: string): string; overload; static;
        class function Replace(const Input, Pattern: string; Evaluator: TMatchEvaluator): string; overload; static;
        class function Replace(const Input, Pattern, Replacement: string; Options: TRegExOptions): string; overload; static;
        class function Replace(const Input, Pattern: string; Evaluator: TMatchEvaluator; Options: TRegExOptions): string; overload; static;
    
        function Split(const Input: string): TArray<string>; overload; inline;
        function Split(const Input: string; Count: Integer): TArray<string>; overload; inline;
        function Split(const Input: string; Count, StartPos: Integer): TArray<string>; overload;
        class function Split(const Input, Pattern: string): TArray<string>; overload; static;
        class function Split(const Input, Pattern: string; Options: TRegExOptions): TArray<string>; overload; static;
      end;

    以 Replace 为例,

    ……

  • 相关阅读:
    [Noip2017]逛公园
    [NOI2005]瑰丽华尔兹
    codeforces 558E A Simple Task
    bzoj1812 riv(树形背包)
    bzoj 1009 GT考试
    bzoj1030 文本生成器 Trie图+dp
    bzoj1500 维修数列(splay)
    [NOI2008]假面舞会
    测试用例的基本知识
    使用Xmind编写测试用例
  • 原文地址:https://www.cnblogs.com/hieroly/p/3101471.html
Copyright © 2011-2022 走看看