Delphi Regular Expression TPerlRegEx Syntax: Option

Source: Internet
Author: User

// 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 \ must be added \;
By default, \ is added before other characters to be recognized as the character itself;
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];

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.