Getting started with tperlregex Regular Expression controls

Source: Internet
Author: User

// When I introduce methods of the tperlregex class, I will discuss more about references to (subexpressions.

Delphi regular expression syntax (9): Critical match-also known as "pre-search" and "reverse pre-search"
// Match the right side
VaR
Reg: tperlregex;
Begin
Reg: = tperlregex. Create (NiL );

Reg. Subject: = 'delphi 6; Delphi 7; Delphi 2007; Delphi net ';
Reg. RegEx: = 'delphi (? = 2007 )';//? =
Reg. Replacement: = '◆ ';
Reg. replaceall;

Showmessage (Reg. Subject); // return value: Delphi 6; Delphi 7; ◆ 2007; Delphi net

Freeandnil (REG );
End;
--------------------------------------------------------------------------------

// Does not match the right
VaR
Reg: tperlregex;
Begin
Reg: = tperlregex. Create (NiL );

Reg. Subject: = 'delphi 6; Delphi 7; Delphi 2007; Delphi net ';
Reg. RegEx: = 'delphi (?! 2007 )';//?!
Reg. Replacement: = '◆ ';
Reg. replaceall;

Showmessage (Reg. Subject); // return value: ◆ 6; ◆ 7; Delphi 2007; ◆ net

Freeandnil (REG );
End;
--------------------------------------------------------------------------------

// Match the left side
VaR
Reg: tperlregex;
Begin
Reg: = tperlregex. Create (NiL );

Reg. Subject: = '2014, $111,222, $333 ';
Reg. RegEx: = '(? <= ¥)/D {3 }';//? <=
Reg. Replacement: = '◆ ';
Reg. replaceall;

Showmessage (Reg. Subject); // return value: 111,222, ¥ ◆, ¥ ◆

Freeandnil (REG );
End;
--------------------------------------------------------------------------------

// Mismatch left
VaR
Reg: tperlregex;
Begin
Reg: = tperlregex. Create (NiL );

Reg. Subject: = '2014, $111,222, $333 ';
Reg. RegEx: = '(? <! $)/D {3 }';//? <!
Reg. Replacement: = '◆ ';
Reg. replaceall;

Showmessage (Reg. Subject); // return: ◆, ◆, ¥333, ¥444
Freeandnil (REG );
End;

Delphi regular expression syntax (10): Option
// Precaseless: case-insensitive, equivalent to I in other languages
VaR
Reg: tperlregex;
Begin
Reg: = tperlregex. Create (NiL );

Reg. Subject: = 'abc ABC abc ';
Reg. RegEx: = 'abc ';
Reg. Replacement: = '◆ ';

Reg. Options: = [precaseless]; // The options are of the set type.

Reg. replaceall;

Showmessage (Reg. Subject); // return: ◆

Freeandnil (REG );
End;
--------------------------------------------------------------------------------

// Preanchored: starts with matching only strings, which is equivalent to ^, but there are still differences.
VaR
Reg: tperlregex;
Begin
Reg: = tperlregex. Create (NiL );

Reg. Subject: = 'abc ABC abc ';
Reg. RegEx: = 'abc ';
Reg. Replacement: = '◆ ';

Reg. Options: = [preanchored]; // specify: preanchored

Reg. replaceall;

Showmessage (Reg. Subject); // return: ◆ ABC

Freeandnil (REG );
End;
{Preanchored options and ^ options:
1. In any case, preanchored only matches the start of the string;
2. In premultiline option mode, ^ matches the beginning of each line;
3. ^ has other functions.
}
--------------------------------------------------------------------------------

// Predollarendonly: make $ match only the end of the string
VaR
Reg: tperlregex;
Begin
Reg: = tperlregex. Create (NiL );

Reg. Subject: = 'abc ABC abc' #13 #10 +
'Abc ABC abc' #13 #10 +
'Abc ABC abc ';
Reg. RegEx: = 'abc $ '; // Of course, $
Reg. Replacement: = '◆ ';

Reg. Options: = [predollarendonly]; // specify: predollarendonly

Reg. replaceall;

Showmessage (Reg. Subject );
{Return:
ABC
ABC
ABC ◆
}

Freeandnil (REG );
End;
--------------------------------------------------------------------------------

// Premultiline: multi-line matching, equivalent to m in other languages
VaR
Reg: tperlregex;
Begin
Reg: = tperlregex. Create (NiL );

Reg. Subject: = 'abc ABC abc' #13 #10 +
'Abc ABC abc' #13 #10 +
'Abc ABC abc ';
Reg. RegEx: = '^ abc ';
Reg. Replacement: = '◆ ';

Reg. Options: = [premultiline]; // specify premultiline.

Reg. replaceall;

Showmessage (Reg. Subject );
{Return:
◆ ABC
◆ ABC
◆ ABC
}
{If premultiline is not specified, the system returns:
◆ ABC
ABC
ABC
}

Freeandnil (REG );
End;
{
1. premultiline is an extension of ^ and $;
2. It is invalid if [preanchored] or [predollarendonly] is specified.
}
--------------------------------------------------------------------------------

// Presingleline: enables special characters to match line breaks (. Intended to match any character other than line breaks)
VaR
Reg: tperlregex;
Begin
Reg: = tperlregex. Create (NiL );

Reg. Subject: = 'aaa; BBB; '#13 #10 +
'2017; 111; '#13 #10 +
'Aaa; BBB ;';
Reg. RegEx: = ';.';
Reg. Replacement: = '◆ ';

Reg. Options: = [presingleline]; // specify presingleline.

Reg. replaceall;

Showmessage (Reg. Subject );
{Return:
AAA ◆ BB ◆
111 ◆ 22 ◆
AAA ◆ BB;
}
{If premultiline is not specified, the system returns:
AAA ◆ BB;
111 ◆ 22;
AAA ◆ BB;
}

Freeandnil (REG );
End;
--------------------------------------------------------------------------------

// Preungreedy: Specifies the non-Greedy mode.
VaR
Reg: tperlregex;
Begin
Reg: = tperlregex. Create (NiL );

Reg. Subject: = 'delphi and C ++ builder ';
Reg. RegEx: = '.. * ';

Reg. Replacement: = '◆ ';

Reg. Options: = [preungreedy]; // specify preungreedy.
{In this example, Reg. RegEx: = '. *?' '; Can achieve the same effect}

Reg. replaceall;

Showmessage (Reg. Subject); // return: ◆ and ◆; If preungreedy is not specified, return: ◆

Freeandnil (REG );
End;
--------------------------------------------------------------------------------

// Preextended: Specify the extended mode.
VaR
Reg: tperlregex;
Begin
Reg: = tperlregex. Create (NiL );

Reg. Subject: = 'delphi C ++ builder ';
Reg. RegEx: = 'I/x20c'; // x20 is a space in hexadecimal format.

Reg. Replacement: = '◆ ';

Reg. Options: = [preextended]; // specify preextended.

Reg. replaceall;

Showmessage (Reg. Subject); // return value: Delph ◆ ++ Builder

Freeandnil (REG );
End;
{Preextended is the most complex option:
1. It ignores the white space in the expression. For example, if the expression in this example is 'I C', it will not match successfully;
2. the blank space must be expressed in hexadecimal notation, for example,/x20;
3. The part in the expression from # To the end of the line is ignored as a comment;
4. If you want to use #, replace it;
5. The standard comment of the expression is :(? #...) And # The content following it is a comment, regardless of whether preextended or not is specified.
}
--------------------------------------------------------------------------------
Preextra:

If the expression requires special characters ^ $ () [] {}.? + * |/, The escape character/is required /;
By default, the characters added before other characters are recognized as the characters themselves;
Preextra should be disabled, that is, do not add/before non-special characters /;
But the test results... maybe I didn't understand it!

There are also three status options: prenotbol, prenoteol, prenotempty

Reg. State: = [prenotbol] indicates that the ^ starting with the flag is invalid;
Reg. State: = [prenoteol] indicates that $ at the end of the tag is invalid;
Reg. State: = [prenotempty] Not clear!

In addition, these options can be used in combination, for example:

Reg. Options: = [precaseless, premultiline, presingleline];
Reg. State: = [prenotbol, prenoteol];

Attributes and methods of tperlregex in Delphi regular expression (1): Search
// Search for existence
VaR
Reg: tperlregex;
Begin
Reg: = tperlregex. Create (NiL );

Reg. Subject: = 'codegear Delphi 2007 for Win32 ';
Reg. RegEx: = '/d ';

If Reg. match then
Showmessage ('found ')
Else
Showmessage ('not found ');

Freeandnil (REG );
End;
--------------------------------------------------------------------------------

// Search for existence (method 2)
VaR
Reg: tperlregex;
Begin
Reg: = tperlregex. Create (NiL );

Reg. Subject: = 'codegear Delphi 2007 for Win32 ';
Reg. RegEx: = '/d ';

Reg. Match; // execute search

If Reg. foundmatch then // The Boolean variable foundmatch will tell us whether the search has any results.
Showmessage ('found ')
Else
Showmessage ('not found ');

Freeandnil (REG );
End;
--------------------------------------------------------------------------------

// Display the first one found
VaR
Reg: tperlregex;
Begin
Reg: = tperlregex. Create (NiL );

Reg. Subject: = 'codegear Delphi 2007 for Win32 ';
Reg. RegEx: = '/d ';

If Reg. match then
Showmessage (Reg. matchedexpression) // 2
Else
Showmessage ('not found ');

Freeandnil (REG );
End;
--------------------------------------------------------------------------------

// Display each and the total numbers respectively.
VaR
Reg: tperlregex;
Num: integer; // use num to count
Begin
Reg: = tperlregex. Create (NiL );

Reg. Subject: = 'codegear Delphi 2007 for Win32 ';
Reg. RegEx: = '/d ';

Num: = 0;
While Reg. matchagain do // matchagain is the next
Begin
Showmessage (Reg. matchedexpression); // the following values are displayed: 2 0 0 7 3 2.
INC (Num );
End;
Showmessage (inttostr (Num); // 6

Freeandnil (REG );
End;
--------------------------------------------------------------------------------

// Display each and all found items respectively (another way)
VaR
Reg: tperlregex;
Num: integer; // use num to count
Begin
Reg: = tperlregex. Create (NiL );

Reg. Subject: = 'codegear Delphi 2007 for Win32 ';
Reg. RegEx: = '/d ';

Num: = 0;
If Reg. match then
Begin
Repeat
Showmessage (Reg. matchedexpression); // the following values are displayed: 2 0 0 7 3 2.
INC (Num );
Until (not Reg. matchagain );
End;
Showmessage (inttostr (Num); // 6

Freeandnil (REG );
End;
--------------------------------------------------------------------------------

// Position and length of the target string
VaR
Reg: tperlregex;
Begin
Reg: = tperlregex. Create (NiL );

Reg. Subject: = 'codegear Delphi 2007 for Win32 ';
Reg. RegEx: = 'delphi ';

While Reg. matchagain do // obviously: only one result can be found in this example.
Begin
Showmessage (Reg. matchedexpression); // The string found: 2007
Showmessage (inttostr (Reg. matchedexpressionoffset); // Its location: 10
Showmessage (inttostr (Reg. matchedexpressionlength); // Its length: 6
End;

Freeandnil (REG );
End;

Attributes and methods of tperlregex in Delphi Regular Expressions (2): subexpressions
// Matchedexpression and subexpressions [0]
VaR
Reg: tperlregex;
Begin
Reg: = tperlregex. Create (NiL );

Reg. Subject: = 'codegear Delphi 2007 ';
Reg. RegEx: = 'delphi ';

While Reg. matchagain do
Begin
Showmessage (Reg. matchedexpression); // Delphi; this is the matched content.
Showmessage (Reg. subexpressions [0]); // Delphi; the Matching content can also be displayed.
End;
{
Subexpressions is an array:
Subexpressions [1] stores content matched by 1st expressions;
Subexpressions [2] stores content matched by 2nd expressions;
Subexpressions [N] stores the content that matches the nth expression;

Subexpressions [0] stores the content matching the entire expression;

Matchedexpression only indicates subexpressions [0].
}

Freeandnil (REG );
End;
--------------------------------------------------------------------------------

// Extract the content matched by the sub-expression
VaR
Reg: tperlregex;
Begin
Reg: = tperlregex. Create (NiL );

Reg. Subject: = 'abc a1111 bb222 ccc33 dddddd4 ';
Reg. RegEx: = '/B ([A-D] +) ([1-4] +)/B'; // This expression has two subexpressions

While Reg. matchagain do
Begin
Showmessage (Reg. subexpressions [0]); // It will be displayed as follows: a1111 bb222 ccc33 dddddd4
Showmessage (Reg. subexpressions [1]); // It is displayed as a bb ccc dddd.
Showmessage (Reg. subexpressions [2]); // 1111 222 33 4

{In addition:
Reg. subexpressioncount is the number of subexpressions;
Reg. subexpressionlengths [N] is the length of the string returned by the nth expression;
Reg. subexpressionoffsets [N] is the position of the string returned by the nth expression in the source string.
}
End;

Freeandnil (REG );
End;
--------------------------------------------------------------------------------

// The number of subexpressions cannot exceed max_subexpressions = 99. max_subexpressions is the built-in constant of tperlregex.

Attributes and methods of tperlregex in Delphi Regular Expression (3): Start and Stop
// Set the search range: Start and Stop
VaR
Reg: tperlregex;
Begin
Reg: = tperlregex. Create (NiL );

Reg. Subject: = 'ababab ';
Reg. RegEx: = 'AB ';
Reg. Replacement: = '◆ ';

Reg. Start: = 1;
Reg. Stop: = 2;
While Reg. matchagain do
Begin
Reg. Replace;
End;
Showmessage (Reg. Subject); // return: ◆ Abab

Reg. Subject: = 'ababab ';
Reg. Start: = 3;
Reg. Stop: = 4;
While Reg. matchagain do
Begin
Reg. Replace;
End;
Showmessage (Reg. Subject); // return: AB ◆ AB

Reg. Subject: = 'ababab ';
Reg. Start: = 5;
Reg. Stop: = 6;
While Reg. matchagain do
Begin
Reg. Replace;
End;
Showmessage (Reg. Subject); // return value: Abab ◆

Freeandnil (REG );
End;

Attributes and methods of tperlregex in Delphi Regular Expression (4): replace
// Replace
VaR
Reg: tperlregex;
Begin
Reg: = tperlregex. Create (NiL );

Reg. RegEx: = 'AB ';
Reg. Replacement: = '◆ ';

Reg. Subject: = 'ababab ';
Reg. replaceall;
Showmessage (Reg. Subject); // return: ◆

Reg. Subject: = 'ababab ';
// The following four rowsProgram, Which is equivalent to Reg. replaceall;
While Reg. matchagain do
Begin
Reg. Replace;
End;

Showmessage (Reg. Subject); // return: ◆

Freeandnil (REG );
End;
{
The replaceall function returns a Boolean value;
The replace function returns the value of replacement. Of course, the value cannot be assigned. It only returns the value.
}

Attributes and methods of the tperlregex class using Delphi Regular Expressions (5): Compile and study
// Compile and study
VaR
Reg: tperlregex;
Begin
Reg: = tperlregex. Create (NiL );

Reg. RegEx: = 'AB ';
Reg. Options: = [precaseless];
Reg. Compile; {compile expression}
Reg. Study; {study method checks whether to compile. If not, execute compile}

Reg. Replacement: = '◆ ';
Reg. Subject: = 'ababab ';

Reg. replaceall;
Showmessage (Reg. Subject); {return value: ◆}

Freeandnil (REG );
End;

{
Compiling expressions will speed up execution and reduce startup speed;
If the expression is complex and executed multiple times, compile it first;
The compilation content includes expression options.
}

Attributes and methods of tperlregex in Delphi Regular Expression (6): escaperegexchars Function
// The escaperegexchars function can automatically add escape characters to special characters/
VaR
Reg: tperlregex;
Begin
Reg: = tperlregex. Create (NiL );

Reg. Subject: = 'C ++ builer ';
Reg. RegEx: = reg. escaperegexchars ('C + ') +' {2} '; {equivalent to 'C/+ {2 }'}
Reg. Replacement: = '◆ ';
Reg. replaceall;

Showmessage (Reg. Subject); {return: ◆ builer}

Freeandnil (REG );
End;

Attributes and methods of tperlregex in Delphi Regular Expression (7): Split Function
// String segmentation: Split
VaR
Reg: tperlregex;
List: tstrings;
Begin
List: = tstringlist. Create;
Reg: = tperlregex. Create (NiL );

Reg. Subject: = 'aaa, BBB, CCC, DDD ';
Reg. RegEx: = ','; {This is a complicated delimiter}

Reg. Split (list, maxint); {the first parameter reads subject, and the second parameter reads subject}
{Enter a maximum integer, indicating the number of minutes}

Showmessage (list. Text );
{Return:
Aaa
Bbb
CCC
Ddd
}

Freeandnil (REG );
List. Free;
End;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.