} } }

    Delphi 正则表达式

    添加时间:2013-5-28 点击量:

    在Delphi中应用正则表达式,我以前用的是PaxScripter里面的——TRegExpr, 引用单位:RegExpr1。但有很多人应用的是RegExpr 是http://RegExpStudio.com   供给的,如今如同免费了,并且包含在Lazarus里面。 Freepascal本身还供给了Regex单位和TRegexEngine类用以解析正则表达式。

    Delphi自2009版开端供给了原生态的正则表达式支撑,而后XE版如同有有所调剂,但都是经由过程应用RegularExpressions单位,但与以前第三方办法有区此外是,它的实现是经由过程Record布局体而不是Class类(万一博客演示2009版,也是Class ——TRegex,并共同IRegex接口 ),据说是为了效力。

    首要功能实现是经由过程应用封装后的类TRegEx,对于所有功能实现它定义了重载后的办法和静态类办法,如下:

     


      TMatchEvaluator = functionconst 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 为例,



    ……

    原来,再大的房子,再大的床,没有相爱的人陪伴,都只是冰冷的物质。而如果身边有爱人陪伴,即使房子小,床小,也觉得无关紧要,因为这些物质上面有了爱的温度,成了家的元素。—— 何珞《婚房》#书摘#
    分享到: