在Delphi中使用正則表達式,我以前用的是PaxScripter裡面的——TRegExpr, 引用單元:RegExpr1。但有很多人使用的是RegExpr 是http://RegExpStudio.com 提供的,現在好像免費了,而且包含在Lazarus裡面。 Freepascal本身還提供了Regex單元和TRegexEngine類用以解析正則表達式。
Delphi自2009版開始提供了原生態的正則表達式支持,而後XE版好像有有所調整,但都是通過應用RegularExpressions單元,但與以前第三方方法有區別的是,它的實現是通過Record結構體而不是Class類(萬一博客演示2009版,也是Class ——TRegex,並配合IRegex接口 ),據說是為了效率。
主要功能實現是通過使用封裝後的類TRegEx,對於所有功能實現它定義了重載後的方法和靜態類方法,如下:
TMatchEvaluator = ( Match: TMatch): ;
TRegEx =
private
FOptions: TRegExOptions;
FMatchEvaluator: TMatchEvaluator;
FNotifier: IInterface;
FRegEx: TPerlRegEx;
InternalOnReplace(Sender: TObject; ReplaceWith: UTF8String);
public
Create( Pattern: ); ;
Create( Pattern: ; Options: TRegExOptions); ;
IsMatch( Input: ): Boolean; ;
IsMatch( Input: ; StartPos: Integer): Boolean; ;
class IsMatch( Input, Pattern: ): Boolean;; static;
class IsMatch( Input, Pattern: ; Options: TRegExOptions): Boolean; ; static;
class Escape( Str: ; UseWildCards: Boolean = False): ; static;
Match( Input: ): TMatch; ;
Match( Input: ; StartPos: Integer): TMatch; ;
Match( Input: ; StartPos, Length: Integer): TMatch; ;
class Match( Input, Pattern: ): TMatch; ; static;
class Match( Input, Pattern: ; Options: TRegExOptions): TMatch; ; static;
Matches( Input: ): TMatchCollection; ;
Matches( Input: ; StartPos: Integer): TMatchCollection; ;
class Matches( Input, Pattern: ): TMatchCollection; ; static;
class Matches( Input, Pattern: ; Options: TRegExOptions): TMatchCollection; ; static;
Replace( Input, Replacement: ): ; ;
Replace( Input: ; Evaluator: TMatchEvaluator): ; ;
Replace( Input, Replacement: ; Count: Integer): ; ;
Replace( Input: ; Evaluator: TMatchEvaluator; Count: Integer): ; ;
class Replace( Input, Pattern, Replacement: ): ; ; static;
class Replace( Input, Pattern: ; Evaluator: TMatchEvaluator): ; ; static;
class Replace( Input, Pattern, Replacement: ; Options: TRegExOptions): ; ; static;
class Replace( Input, Pattern: ; Evaluator: TMatchEvaluator; Options: TRegExOptions): ; ; static;
Split( Input: ): TArray<>; ; ;
Split( Input: ; Count: Integer): TArray<>; ; ;
Split( Input: ; Count, StartPos: Integer): TArray<>; ;
class Split( Input, Pattern: ): TArray<>; ; static;
class Split( Input, Pattern: ; Options: TRegExOptions): TArray<>; ; static;
;
以 Replace 為例,
……