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 为例,

……

原文地址:https://www.cnblogs.com/hieroly/p/3101471.html