Delphi regular expressions

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

The use of regular expressions in Delphi, I used to use the PaxScripter inside -- TRegExpr, reference unit: RegExpr1. But there are a lot of people are using RegExpr is provided, it seems free of charge, but also included in the Lazarus. Freepascal also provides the Regex unit and the TRegexEngine class to regular expressions.

Delphi provides regular expression original ecological support since version 2009, then the XE version seems to be adjusted somewhat, but is through the application of RegularExpressions unit, but with the previous third methods differ is, it is realized by the Record structure rather than the Class class (million blog demo version 2009, is Class -- TRegex, and with IRegex interface), is said to efficiency.

The main function is realized by using the package after the TRegEx, for all the functions which define the method after loading and static class methods, as follows:

 
  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;

In the case of Replace,

...

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Lyndon at December 14, 2013 - 12:56 PM