Regularexpressions (3) working ideas of regularexpressions

Source: Internet
Author: User
Assume there is such a string of text: aaa1 bbb2 aa11 bb22 a111 b222 AAAA

You can use the expression [A-Za-Z] + \ D + to extract the first six strings. TestCode:

 
Uses regularexpressions; Procedure tform1.formcreate (Sender: tobject); var RegEx: iregex; {regularexpressions advocates iregex instead of tregex} match: imatch; {imatch indicates one of the matched strings} begin {build expression} RegEx: = tregex. create ('[A-Za-Z] + \ D +'); {the first string matching the expression is matched.} match: = RegEx. match ('aaa1 bbb2 aa11 bb22 a111 b222 aaa'); {match is available. value to obtain the matched text. here it will be aaa1} {match is available. length: Get the length of the text. here it will be: 4} {match is available. index to obtain the position of the string in the original string; here it will be: 1, and the second will be 6} {belowProgramAll matched substrings will be traversed} while match. success do {imatch. success: Successful match} begin showmessage (match. value); match: = match. nextmatch; {imatch. nextmatch: Continue to match the next one, and return the next imatch object} end;

  

Still face the text above: aaa1 bbb2 aa11 bb22 a111 b222 AAAA

If you want to extract "letters" or "Numbers" based on the previous match, you must use a subexpression;

For example, the expression can be written as follows: ([A-Za-Z] +) (\ D +)

The matched subexpressions are placed in imatch. Groups;
Groups is a collection of igroup objects. Its total number of elements = number of subexpressions + 1. Example:

 
Uses regularexpressions; Procedure tform1.formcreate (Sender: tobject); var RegEx: iregex; match: imatch; s, W, N: string; begin RegEx: = tregex. create ('([A-Za-Z] +) (\ D +)'); match: = RegEx. match ('aaa1 bbb2 aa11 bb22 a111 b222 aaa'); While match. success do begin S: = match. groups [0]. value; {groups [0] match result for placing the entire expression} W: = match. groups [1]. value; {groups [1] match result for placing the first subexpression} n: = match. groups [2]. value; {groups [2] match result for placing the second subexpression} showmessagefmt ('substring: % s; letter: % s; number: % s', [s, W, n]); match: = match. nextmatch; end; (* sub-string: aaa1; letter: AAA; number: 1 sub-string: bbb2; letter: BBB; number: 2 sub-string: aa11; letter: AA; number: 11 substring: bb22; letter: BB; number: 22 substring: a111; letter: A; number: 111 substring: b222; letter: B; Number: 222 *) end;

  

Through regular expressions, we mainly do two jobs: search and replace. The above is the basic idea of searching. Let's look at replacement again. The example is directly:

 
Uses regularexpressions; Procedure tform1.formcreate (Sender: tobject); var RegEx: iregex; STR: string; begin RegEx: = tregex. create ('([A-Za-Z] +) (\ D +)'); {Replace the text matched by the expression with ◆} STR: = RegEx. replace ('aaa1 bbb2 aa11 bb22 a111 b222 aaa', '◆ '); showmessage (STR ); {◆ AAAA} {Replace the text matched by the expression with "matching result of the first subexpression"} STR: = RegEx. replace ('aaa1 bbb2 aa11 bb22 a111 b222 aaa', '$ 1'); showmessage (STR ); {aaa bbb AA bb a B AAAA} {Replace the text matched by the expression with "matching result of the second subexpression", which is equivalent to deleting the number} STR: = RegEx. replace ('aaa1 bbb2 aa11 bb22 a111 b222 aaa', '$ 2'); showmessage (STR); {1 2 11 22 111 222 AAAA} end;

  

Use $1 $2 in replacement. The following lists all similar references:

$ N // the nth subexpression. $0 indicates the entire expression $ & // with $0 $ + // The Last subexpression; in the above example, it is equivalent to $2 $ _ // replacing the entire input string $ '// replacing all text of the matching input string $' // replacing all of the matched input strings text; this is written in Delphi: $ ''$ // Since $ is used for escape, if you need this symbol itself, You Can $ <Name> // specify the name of the subexpression; the current version is not supported.

  

Not all of the above, but it is already the main context; other content also focuses on this idea.

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.