Copyright: Copyright © 1999, 2005, 2008, 2010 Jan Goyvaerts


PerlRegEx unit

See also

TPerlRegEx is a Delphi VCL wrapper around the open source PCRE library, which implements Perl-Compatible Regular Expressions.

This version of TPerlRegEx is compatible with the TPerlRegEx class in the RegularExpressionsCore unit in Delphi XE.  In fact, the unit in Delphi XE is derived from the version of TPerlRegEx that you are using now.

The supplied pcrelib.dll contains PCRE 7.9, compiled with Unicode support.  The supplied OBJ files contain PCRE 7.9, compiled with Unicode support.  By default, the OBJ files are used.  You can use the DLL if you have multiple applications using TPerlRegEx and you don't want to waste space by linking the OBJ files to be linked into each of those applications.

For more information about PCRE, please visit http://www.regular-expressions.info/pcre.html

For more information about regular expressions in general, please visit http://www.regular-expressions.info/

You can download the latest version of TPerlRegEx at http://www.regular-expressions.info/delphi.html

TPerlRegEx is licensed under the Mozilla Public License, version 1.1.

This help file only describes the TPerlRegEx class itself.

Classes

TPerlRegEx

TPerlRegExList

Types

TPerlRegExOptions

TPerlRegExReplaceEvent

TPerlRegExState

Constants

MAX_GroupS


PerlRegEx unit - See also
See also

TPerlRegEx class


TPerlRegEx class

Properties	Methods	Events

Unit

PerlRegEx

Description

The TPerlRegEx class aimes at providing any Delphi or C++Builder developer with the same, powerful regular expression capabilities provided by the Perl programming language, created by Larry Wall.  It is implemented as a wrapper around the open source PCRE library.

Note that this help file assumes that you know what regular expressions are and what you can do with them.  If you don't, you can find a very detailed tutorial about regular expressions at http://www.regular-expressions.info/

Tasks

•	Declasre a variable of type TPerlRegEx and call its constructor.

•	Set the Options property

•	Assign a regular expression to the RegEx property if you did not do so at design time.

•	If you want to do replacements, attach an OnReplace event handler or put a string in the Replacement property

•	Assign a string to the Subject property

•	Call the Match method

•	If you assigned an OnReplace event handler or the Replacement property and Match returned true, call the Replace method

•	Call the MatchAgain method if Match returned true and you want to continue searching

•	Instead of calling Match, MatchAgain and Replace, call the ReplaceAll method to do a global substitution in the Subject string, like Perl's s///g operator.


TPerlRegEx - Properties
Properties

Compiled

FoundMatch

Match

MatchLength

MatchOffset

Options

RegEx

Replacement

Start

State

Stop

Studied

GroupCount

GroupLengths

GroupOffsets

Groups

Subject


TPerlRegEx - Methods
Methods

Compile

ComputeReplacement

EscapeRegExChars

Match

MatchAgain

NamedGroup

Replace

ReplaceAll

Split method

StoreGroups

Study


TPerlRegEx - Events
Events

OnMatch

OnReplace


Compiled property

See also

Applies to

TPerlRegEx class

Declaration

property Compiled: Boolean;

Description

True if the RegEx has already been compiled by calling Compile.

Read-only.


Compiled property - See also
See also

Compile method


FoundMatch property

See also

Applies to

TPerlRegEx class

Declaration

property FoundMatch: Boolean;

Description

Returns True when Match and Groups indicate a match.

Read-only.


FoundMatch property - See also
See also

Match property

Groups property

Match method

MatchAgain method


Match property

See also	Example

Applies to

TPerlRegEx class

Declaration

property Match: string;

Description

The substring matched by the regular expression since the last call to the Match method or the MatchAgain method.

Read-only.


Match property - See also
See also

FoundMatch property

MatchLength property

MatchOffset property

Groups property

Subject property

Match method

MatchAgain method


Match property - Example
Match property example

This example assumes you placed a TLabel and a TPerlRegEx on a form, and that PerlRegEx.Subject is set to a meaningful test string.

with PerlRegEx1 do begin

  RegEx := 'Windows|Linux';  // Matches 'Windows' or 'Linux', whichever comes first

  if Match then Label1.Caption := Match + ' rulez!';

end;


MatchLength property

See also	Example

Applies to

TPerlRegEx class

Declaration

property MatchLength: Integer;

Description

Length of the substring matched by the regular expression since the last call to Match method or MatchAgain method.  This is a shortcut to Length(Match) and will execute much faster.

Read-only.


MatchLength property - See also
See also

FoundMatch property

Match property

MatchOffset property

Groups property

GroupLengths property

Subject property

Match method

MatchAgain method


MatchLength property - Example
MatchLength property example

This example assumes you placed a TLabel and a TPerlRegEx on a form.

with PerlRegEx1 do begin

  Subject := 'Rating: AAA';

  RegEx := 'A+';  // Matches the first occurence of 'A', including any A's that may follow right after it

  if Match then Label1.Caption :=

    'You have been rated with ' + IntToStr(MatchLength) + 'A''s'

  else

    Label1.Caption := 'You have a poor rating';

end;


MatchOffset property

See also	Example

Applies to

TPerlRegEx class

Declaration

property MatchOffset: Integer;

Description

Character offset in the Subject string at which the matched substring starts.

Read-only.


MatchOffset property - See also
See also

FoundMatch property

Match property

MatchLength property

Groups property

GroupOffsets property

Subject property

Match method

MatchAgain method


MatchOffset property - Example
MatchOffset property example

This example assumes you placed a TLabel and a TPerlRegEx on a form, and that PerlRegEx.Subject is set to a meaningful test string.

with PerlRegEx1 do begin

  RegEx := 'Bye';

  if Match then Label1.Caption := 'Before typing "Bye", you typed: ' +

    Copy(Subject, 1, MatchOffset-1);

end;


Options property

See also

Applies to

TPerlRegEx class

Declaration

property Options: TPerlRegExOptions;

Description

These are the most important options you can specify:

preCaseLess	Tries to match the regex without paying attention to case.  If set, 'Bye' will match 'Bye', 'bye', 'BYE' and even 'byE', 'bYe', etc.  Otherwise, only 'Bye' will be matched.  Equivalent to Perl's /i modifier.

preMultiLine	The ^ (beginning of string) and $ (ending of string) regex operaters will also match right after and right before a newline in the Subject string.  This effectively treats one string with multiple lines as multiple strings.  Equivalent to Perl's /m modifier.

preSingleLine Normally, dot (.) matches anything but a newline (\n).  With preSingleLine, dot (.) will match anything, including newlines.  This allows a multiline string to be regarded as a single entity.  Equivalent to Perl's /s modifier.

Note that preMultiLine and preSingleLine can be used together.

preExtended	Allow regex to contain extra whitespace, newlines and Perl-style comments, all of which will be filtered out.  This is sometimes called "free-spacing mode".

preAnchored	Allows the regex to match only at the start of the subject or right after the previous match.

preUngreedy	Repeat operaters (?, *, +, {num,num}) are greedy by default, i.e. they try to match as many characters as possible.  Set preUngreedy to use ungreedy repeat operators by default, i.e. they try to match as few characters as possible.


Options property - See also
See also

State property

TPerlRegExOptions type


RegEx property

See also	Example

Applies to

TPerlRegEx class

Declaration

property RegEx: string;

Description

The regular expression to be matched.  See http://www.regular-expressions.info/ to learn how to write a regular expression.

The TPerlRegEx class uses the "PCRE" regular expression flavor explained at http://www.regular-expressions.info/refflavors.html.


RegEx property - See also
See also

Subject property

Match method


RegEx property - Example
RegEx property example

with PerlRegEx1 do begin

  RegEx := 'Perl';

  Subject := 'Another example for TPerlRegEx';

  if Match then ShowMessage('We found another Perl!');

end;


Replacement property

See also	Example

Applies to

TPerlRegEx class

Declaration

property Replacement: string;

Description

String to replace matched expression with. Backreferences \0, \1, \2 etc. as well as $1, $2, $3, etc. will be substituted with Groups[number].

The TPerlRegEx class uses the "JGsoft" replacement text flavor explained at http://www.regular-expressions.info/refreplace.html.


Replacement property - See also
See also

RegEx property

Subject property

Groups property

Replace method

ReplaceAll method

OnReplace event


Replacement property - Example
Replacement property example

This example will show a messagebox saying "Foo is the name of the bar I like".

with PerlRegEx1 do begin

  Subject := 'I like to hang out at Foo bar';

  RegEx := '([A-Za-z]+) bar';

  Replacement := '\1 is the name of the bar I like';

  if Match then Replace;

  ShowMessage(RegEx.Subject);

end;


Start property

See also	Example

Applies to

TPerlRegEx class

Declaration

property Start: Integer;

Description

Starting position in Subject from which MatchAgain begins.

By default, MatchAgain continues from the end of the previous match, or from the start of the subject if there is no previous match.  Set the Start property to continue searching from another position.


Start property - See also
See also

Stop property

Subject property

MatchAgain method


State property

See also	Example

Applies to

TPerlRegEx class

Declaration

property State: TPerlRegExState;

Description

State of the Subject string.

preNotBOL	Do not match ^ or \A at the start of the subject string.  Set this state if the subject string is not the logical beginning of the data the user is working with.

preNotEOL	Do not match $ or \Z at the end of the subject string.  Set this state if the subject string is not the logical end of the data the user is working with.

preNotEmpty	Do not match an empty string, i.e. if Match returns true, Match will never be an empty string.  This can be useful if everything in your regular expression is optional (e.g.: a?b?c?).  The match returned is normally the leftmost, longest match.  If the subject is "dabdabc", it would match at the empty string right before "d".  With this option, the regex will match "ab" (chars 2 and 3 in the subject), the leftmost, longest match that is not an empty string.  MatchAgain will match "abc" at the end of the subject.


State property - See also
See also

Subject property

Options property

TPerlRegExState type


State property - Example
State property example

You can implement a "find from cursor" function as follows:

PerlRegEx1.Subject := Data.GetString(CursorPosition, Data.DataLength);

PerlRegEx1.State := [preNotBOL];

PerlRegEx1.Match;


Stop property

See also	Example

Applies to

TPerlRegEx class

Declaration

property Stop: Integer;

Description

Last character in Subject that Match and MatchAgain search through.  By default, they search until the end of the string.  Use the Stop property to search through only part of the string without having to reallocate a truncated string.


Stop property - See also
See also

Start property

Subject property

MatchAgain method


Stop property - Example
Stop property example

Search through characters 100..200 in S this way:

PerlRegEx1.Subject := S;

PerlRegEx1.Start := 100;

PerlRegEx1.Stop := 200;

PerlRegEx1.MatchAgain;

This is faster than:

PerlRegEx1.Subject := Copy(S, 100, 100);

PerlRegEx1.MatchAgain;

since the former method does not allocate any memory to keep a duplicate of S.


Studied property

See also

Applies to

TPerlRegEx class

Declaration

property Studied: Boolean;

Description

Read-only.

True if the RegEx has already been studied by the Study method.

Calling Study when Studied is already True, has no effect.


Studied property - See also
See also

RegEx property

Compile method

Study method


GroupCount property

See also

Applies to

TPerlRegEx class

Declaration

property GroupCount: Integer;

Description

Read-only.

Number of matched Groups stored in the Groups array.  This number is the number of the highest-numbered capturing group in your regular expression that actually participated in the last match.  It may be less than the number of capturing groups in your regular expression.

E.g. when the regex "(a)|(b)" matches "a", GroupCount will be 1.  When the same regex matches "b", GroupCount will be 2.


GroupCount property - See also
See also

GroupOffsets property

Groups property


GroupLengths property

See also

Applies to

TPerlRegEx class

Declaration

property GroupLengths[Index: Integer: Integer;

Description

Read-only.

Lengths of the Group strings in the Groups array.

Querying GroupLengths[Index] is faster than Length(Groups[Index]), since the former only needs to do a few integer computations, while the latter actually extracts a string from the Subject.

Valid range for Index is 0..GroupCount.


GroupLengths property - See also
See also

GroupCount property

GroupOffsets property

Groups property


GroupOffsets property

See also

Applies to

TPerlRegEx class

Declaration

property GroupOffsets[Index: Integer: Integer;

Description

Read-only.

Character offsets in the Subject string of the Groups strings.

Valid range for Index is 0..GroupCount.

Groups[Index] = Copy(Subject, GroupOffsets[Index], GroupLengths[Index]);


GroupOffsets property - See also
See also

GroupCount property

GroupLengths property

Groups property


Groups property

See also	Example

Applies to

TPerlRegEx class

Declaration

property Groups[Index: Integer: string;

Description

Read-only.

Matched Groups after a regex has been matched.

Groups[0] = Match

The item count in the Groups array increases with one for every opening bracket in the regex.  The text matched by the part of the regex between that opening bracket and its corresponding closing bracket, will be put in the Groups array.

Valid range for Index is 0..GroupCount.  Note that GroupCount need not be equal to the number of opening brackets, since it may happen that only part of the regular expression played a role in the current match.

If you used named capturing groups in the regular expressions, use the NamedGroup method to retrieve the index number of a named group.


Groups property - See also
See also

Match property

GroupCount property

GroupLengths property

GroupOffsets property

Match method

NamedGroup method

StoreGroups method


Groups property - Example
Groups property example

with PerlRegEx1 do begin

  RegEx := '(Joe|Jack|William|Avarell) (Dalton)? did it';

  Subject := 'The sheriff told us that Joe did it.';

  if Match then

    if GroupCount = 2 then

      ShowMessage('Wanted: ' + Groups[1] + ' ' + Groups[2])

    else

      ShowMessage('Wanted: ' + Groups[1] + ' (probably Dalton)');

end;


Subject property

See also	Example

Applies to

TPerlRegEx class

Declaration

property Subject: string;

Description

The string on which Match will try to match RegEx.


Subject property - See also
See also

RegEx property

Match method


Subject property - Example
Subject property example

with PerlRegEx1 do begin

  RegEx := Edit1.Text;

  Subject := Edit2.Text;

  if Match then ShowMessage('The regex you entered in Edit1 matches the string in Edit2')

    else ShowMessage('The regex you entered in Edit1 does not match the string in Edit2');

end;


Compile method

See also

Applies to

TPerlRegEx class

Declaration

class procedure Compile;

Description

Before it can be used, the regular expression needs to be compiled.  Match will call Compile automatically if you did not do so.

If the regular expression will be applied in time-critical code, you may wish to compile it during your application's initialization.  You may also want to call Study to further optimize the execution of the regex.


Compile method - See also
See also

Compiled property

Match method

Study method


ComputeReplacement method

See also	Example

Applies to

TPerlRegEx class

Declaration

function ComputeReplacement: string;

Description

Returns Replacement with backreferences found by the last match filled in.  Unlike the Replace method, ComputeReplacement does not modify Subject.


ComputeReplacement method - See also
See also

Replacement property

Replace method


ComputeReplacement method - Example
ComputeReplacement method example

This example will show a messagebox saying "Foo is the name of the bar I like".

with PerlRegEx1 do begin

  Subject := 'I like to hang out at Foo bar';

  RegEx := '([A-Za-z]+) bar';

  Replacement := '\1 is the name of the bar I like';

  if Match then ShowMessage(RegEx.ComputeReplacement);

end;


EscapeRegExChars method

See also	Example

Applies to

TPerlRegEx class

Declaration

function EscapeRegExChars(const S: string): string;

Description

Escapes regex characters in S so that the regex engine can be used to match S as plain text.


EscapeRegExChars method - See also
See also

RegEx property


EscapeRegExChars method - Example
EscapeRegExChars method example

To search for '1+1' as literal text, use:

PerlRegEx1.RegEx := PerlRegEx1.EscapeRegExChars('1+1');

which does the same as:

PerlRegEx1.RegEx := '1\+1';


Match method

See also	Example

Applies to

TPerlRegEx class

Declaration

function Match: Boolean;

Description

Attempts to match the regular expression specified in the RegEx property on the string specified in the Subject property.  If Compile has not yet been called, Match will do so for you.

Call MatchAgain to attempt to match the regex on the remainder of the subject string after a successful call to Match.


Match method - See also
See also

RegEx property

Options property

Subject property

Compile method

Match method

MatchAgain method

OnMatch event


Match method - Example
Match method example

with PerlRegEx1 do begin

  RegEx := Edit1.Text;

  Subject := Edit2.Text;

  if Match then ShowMessage('The regex you entered in Edit1 matches the string in Edit2')

    else ShowMessage('The regex you entered in Edit1 does not match the string in Edit2');

end;


MatchAgain method

See also	Example

Applies to

TPerlRegEx class

Declaration

function MatchAgain: Boolean;

Description

Attempt to match the regex to the remainder of the string after the previous match.  If you assigned the Start property, MatchAgain continues from that position instead.  If not, you should only call MatchAgain after calling Match.


MatchAgain method - See also
See also

Match method

Start property

Stop property

OnMatch event


MatchAgain method - Example
MatchAgain method example

with PerlRegEx1 do begin

  RegEx := Edit1.Text;

  Subject := Edit2.Text;

  if Match then begin

    I := 1;

    while MatchAgain do I := I + 1;

    ShowMessage(Format('The regular expression matched the subject string %d times.', [I]);

  end

  else

    ShowMessage('The regular expression did not match the subject string at all.');

end;


NamedGroup method

See also	Example

Applies to

TPerlRegEx class

Declaration

function NamedGroup(const SEName: string): Integer;

Description

Returns the index of the named group SEName.  Use the returned value with the Groups property to retrieve the text matched by the named group.


NamedGroup method - See also
See also

GroupCount property

GroupLengths property

GroupOffsets property

Groups property


NamedGroup method - Example
NamedGroup method example

with PerlRegEx1 do begin

  RegEx := '(?P<firstname>Joe|Jack|William|Avarell) did it';

  Subject := 'The sheriff told us that Joe did it.';

  if Match then

    ShowMessage('Wanted: ' + Groups[NamedGroup('firstname')]);

end;


Replace method

See also	Example

Applies to

TPerlRegEx class

Declaration

function Replace: string;

Description

Replace matched expression in Subject with ComputeReplacement. Returns the actual replacement text from ComputeReplacement.  You may only call Replace after a successful call to Match or MatchAgain.


Replace method - See also
See also

Replacement property

Subject property

ComputeReplacement method

Match method

MatchAgain method

ReplaceAll method

OnReplace event


Replace method - Example
Replace method example

with PerlRegEx1 do begin

  Subject := 'Bill loves Hillary';

  RegEx := 'Hillary';

  Replacement := 'Monica';

  if Match then Replace;

  ShowMessage(Subject);   // "Bill loves Monica"

end;


ReplaceAll method

See also	Example

Applies to

TPerlRegEx class

Declaration

function ReplaceAll: Boolean;

Description

Replaces all matches of RegEx in Subject with ComputeReplacement. Returns True if anything was replaced at all.


ReplaceAll method - See also
See also

RegEx property

Replacement property

Subject property

Match method

MatchAgain method

Replace method

OnReplace event


ReplaceAll method - Example
ReplaceAll method example

ReplaceAll is the equivalent of:

if Match then begin

  Replace;

  while MatchAgain do Replace;

end;


Split method

See also	Example

Applies to

TPerlRegEx class

Declaration

procedure Split(Strings: TStrings; Limit: Integer);

Description

Splits Subject along regex matches.  The text between each regex match is appended to the list passed in the Strings parameter.  If Limit >= 1, at most Limit strings are appended.


Split method - See also
See also

RegEx property

Subject property

Match method

MatchAgain method


Split method - Example
Split method example

This example appends each item in the comma-delimited list in Edit1 to Memo1:

with PerlRegEx1 do begin

  RegEx := ',';

  Subject := Edit1.Text;

  Split(Memo1.Lines, 0);

end;


StoreGroups method

See also	Example

Applies to

TPerlRegEx class

Declaration

procedure StoreGroups;

Description

Stores duplicates of Groups[] so they and ComputeReplacement will still return the proper strings even if Subject is changed or cleared.

This can be useful if you plan to modify the string that you assigned to Subject.  If you do that without clearing Subject first, Delphi will automatically create a duplicate of the subject string, which wastes memory and CPU time if the subject is very long.


StoreGroups method - See also
See also

Groups property

Subject property


StoreGroups method - Example
StoreGroups method example

The first example displays "Nobody wanted":

S := 'The sheriff told us that Joe did it.';

with PerlRegEx1 do begin

  RegEx := '(Joe|Jack|William|Avarell) did it';

  Subject := S;

  if Match then begin

    Subject := ''; // Save memory

    if RegEx.GroupCount = 1 then

      S := 'Wanted: ' + RegEx.Groups[1]

    else

      S := 'Nobody wanted';

    ShowMessage(S);

  end;

end;

The second example displays "Wanted: Joe".

S := 'The sheriff told us that Joe did it.';

with PerlRegEx1 do begin

  RegEx := '(Joe|Jack|William|Avarell) did it';

  Subject := S;

  if Match then begin

    RegEx.StoreGroups;

    Subject := ''; // Save memory

    if RegEx.GroupCount = 1 then

      S := 'Wanted: ' + RegEx.Groups[1]

    else

      S := 'Nobody wanted';

    ShowMessage(S);

  end;

end;


Study method

See also	Example

Applies to

TPerlRegEx class

Declaration

procedure Study;

Description

Study the regex. Studying takes time, but will make the execution of the regex a lot faster.

Call study if you will be using the same regex many times.

Study will also call Compile if this had not yet been done.


Study method - See also
See also

Compiled property

Studied property

Compile method


Study method - Example
Study method example

Depending on what the user entered in Edit1 and Memo1, RegEx might end up being a pretty complicated regular expression that wil be applied to the memo text a great many times.  This makes it worthwhile to spend a little extra time studying the regular expression.

with PerlRegEx1 do begin

  RegEx := Edit1.Text;

  Study;

  Subject := Memo1.Lines.Text;

  Replacement := Edit2.Text;

  ReplaceAll;

  Memo1.Lines.Text := Subject;

end;


OnMatch event

See also

Applies to

TPerlRegEx class

Declaration

property OnMatch: TNotifyEvent;

Description

Triggered by Match and MatchAgain after a successful match.


OnMatch event - See also
See also

Match method

MatchAgain method


OnReplace event

See also	Example

Applies to

TPerlRegEx class

Declaration

property OnReplace: TPerlRegExReplaceEvent;

Description

Triggered by Replace and ReplaceAll just before the replacement is done, allowing you to determine the new string.


OnReplace event - See also
See also

Replacement property

ComputeReplacement method

Replace method

ReplaceAll method


OnReplace event - Example
OnReplace event example

with PerlRegEx1 do begin

  RegEx := 'Borland|Inprise|Microsoft';

  Subject := Edit1.Text;

  ReplaceAll;

  Edit1.Text := Subject;

end;

procedure TForm1.PerlRegEx1Replace(Sender: TObject; var ReplaceWith: string);

begin

  if PerlRegEx1.Match = 'Inprise' then ReplaceWith := 'Borland'

    else if PerlRegEx1.Match = 'Borland' then ReplaceWith := ':-)'

    else ReplaceWith := ':-('

end;


TPerlRegExList class

See also	Properties	Methods

Unit

PerlRegEx

Description

You can add TPerlRegEx objects to a TPerlRegExList to match them all together on the same subject, as if they were one regex regex1|regex2|regex3|...

TPerlRegExList does not own the TPerlRegEx objects, just like a TList.  If a TPerlRegEx has been added to a TPerlRegExList, it should not be used in any other situation until it is removed from the list.


TPerlRegExList class - See also
See also

TPerlRegEx class


TPerlRegExList - Properties
Properties

Count

MatchedRegEx

RegEx

Start

Stop

Subject


TPerlRegExList - Methods
Methods

 Key methods

Add

IndexOf

Match

MatchAgain

Clear

Delete

Insert


Count property

Applies to

TPerlRegExList class

Declaration

property Count: Integer;

Description

Number of TPerlRegEx objects in the list.

Read-only.


MatchedRegEx property

See also

Applies to

TPerlRegExList class

Declaration

property MatchedRegEx: TPerlRegEx;

Description

Indicates which TPerlRegEx object in the list was responsible for finding the last match.

Read-only.


MatchedRegEx property - See also
See also

Match method

MatchAgain method


RegEx property

See also

Applies to

TPerlRegExList class

Declaration

property RegEx[Index: Integer]: TPerlRegEx;

Description

The regular expression of each TPerlRegEx object in the list.


RegEx property - See also
See also

Count property

Add method


Start property

See also

Applies to

TPerlRegExList class

Declaration

property Start: Integer;

Description

Starting position in Subject from which MatchAgain begins.

By default, MatchAgain continues from the end of the previous match, or from the start of the subject if there is no previous match.  Set the Start property to continue searching from another position.


Start property - See also
See also

Stop property

Subject property

MatchAgain method


Stop property

See also

Applies to

TPerlRegExList class

Declaration

property Stop: Integer;

Description

Last character in Subject that Match and MatchAgain search through.  By default, they search until the end of the string.  Use the Stop property to search through only part of the string without having to reallocate a truncated string.


Stop property - See also
See also

Start property

Subject property

Match method

MatchAgain method


Subject property

See also

Applies to

TPerlRegExList class

Declaration

property Subject: string;

Description

The string on which Match will try to match the regular expressions.


Subject property - See also
See also

Add method

Match method

MatchAgain method


Add method

See also

Applies to

TPerlRegExList class

Declaration

function Add(ARegEx: TPerlRegEx): Integer;

Description

Add a regular expression to the list.


Add method - See also
See also

Delete method

Insert method


IndexOf method

See also

Applies to

TPerlRegExList class

Declaration

function IndexOf(ARegEx: TPerlRegEx): Integer;

Description

Returns the index of the given TPerlRegEx object in the list.  Returns -1 if the list does not contain the object.


IndexOf method - See also
See also

Add method

Delete method

Insert method


Match method

See also

Applies to

TPerlRegExList class

Declaration

function Match: Boolean;

Description

Attempts to match the regular expressions in the list on the string specified in the Subject property.  Call MatchAgain to attempt to match the regex on the remainder of the subject string after a successful call to Match.

If more than one regular expression matches the subject, the leftmost match (i.e. the one with the lowest MatchOffset) is returned by the MatchedRegEx property.  If more than one regular expression matches at the same position in the string, the one added to the list first is returned.  Use the MatchedRegEx property to get more information about the match.


Match method - See also
See also

MatchedRegEx property

MatchAgain method


MatchAgain method

See also

Applies to

TPerlRegExList class

Declaration

function MatchAgain: Boolean;

Description

Attempt to match the regex to the remainder of the string after the previous match.  If you assigned the Start property, MatchAgain continues from that position instead.  If not, you should only call MatchAgain after calling Match.

If more than one regular expression matches the subject, the leftmost match (i.e. the one with the lowest MatchOffset) is returned by the MatchedRegEx property.  If more than one regular expression matches at the same position in the string, the one added to the list first is returned.  Use the MatchedRegEx property to get more information about the match.

Note that MatchAgain continues from the end of the previous match.  Any matches by other regular expressions in the list that overlap with the previous match are ignored.  This is the same behavior as you would get when stringing all the regular expressions together as regex1|regex2|regex3... and using TPerlRegEx.MatchAgain.


MatchAgain method - See also
See also

MatchedRegEx property

Start property

Stop property

Match method


Clear method

See also

Applies to

TPerlRegExList class

Declaration

procedure Clear;

Description

Removes all regular expressions from the list.  The TPerlRegEx objects are NOT freed.


Clear method - See also
See also

Delete method


Delete method

See also

Applies to

TPerlRegExList class

Declaration

procedure Delete(Index: Integer);

Description

Remove a TPerlRegEx object from the list.  The object is NOT freed.


Delete method - See also
See also

Add method

Clear method


Insert method

See also

Applies to

TPerlRegExList class

Declaration

procedure Insert(Index: Integer; ARegEx: TPerlRegEx);

Description

Add a regular expression to the list at a particular position.  The order of the regular expressions in the list matches only if two regular expressions match at the same position in the subject string.


Insert method - See also
See also

Add method

Match method


TPerlRegExOptions type

See also

Unit

PerlRegEx

Declaration

type

  TPerlRegExOptions = set of (

    preCaseLess,       // /i -> Case insensitive

    preMultiLine,      // /m -> ^ and $ also match before/after a newline, not just at the beginning and the end of the string

    preSingleLine,     // /s -> Dot matches any character, including \n (newline). Otherwise, it matches anything except \n

    preExtended,       // /x -> Allow regex to contain extra whitespace, newlines and Perl-style comments, all of which will be filtered out

    preAnchored,       // /A -> Successful match can only occur at the start of the subject or right after the previous match

    preDollarEndOnly,  // /E

    preExtra,          // /X

    preUnGreedy        // Repeat operators (+, *, ?) are not greedy by default

  );                   //   (i.e. they try to match the minimum number of characters instead of the maximum)


TPerlRegExOptions type - See also
See also

TPerlRegEx class

Options property


TPerlRegExReplaceEvent type

See also

Unit

PerlRegEx

Declaration

type TPerlRegExReplaceEvent = procedure(Sender: TObject; var ReplaceWith: string); of object;

Description

TPerlRegEx.OnReplace event type.

Sender is the calling TPerlRegEx object.

ReplaceWith is equal to the string returned by ComputeReplacement method.  The matched expression will be replaced with whatever you assign to ReplaceWith.


TPerlRegExReplaceEvent type - See also
See also

TPerlRegEx class

OnReplace event


TPerlRegExState type

See also

Unit

PerlRegEx

Declaration

  TPerlRegExState = set of (

    preNotBOL,         // Not Beginning Of Line: ^ does not match at the start of Subject

    preNotEOL,         // Not End Of Line: $ does not match at the end of Subject

    preNotEmpty        // Empty matches not allowed

  );


TPerlRegExState type - See also
See also

TPerlRegEx class

State property


MAX_GroupS constant

Unit

PerlRegEx

Declaration

const MAX_GroupS = 99

Description

Maximum number of capturing groups you can use in your regular expression.