Introduction to regular expressions (Microsoft) -- 14. Backward reference

Source: Internet
Author: User

Backward reference
One of the most important features of a regular expression is to store a part of the matched pattern for future use. Recall that adding parentheses on both sides of a regular expression mode or partial mode will cause this expression to be stored in a temporary buffer. Can I use non-captured metacharacters '? :','? = ', Or '?! 'To ignore the storage of this part of the regular expression.
Each captured sub-match is stored in the content from left to right in the regular expression mode. The buffer number that stores the sub-match starts from 1 and ranges from consecutive numbers to a maximum of 99 subexpressions. Each buffer zone can be accessed using '', where n is one or two decimal digits that identify a specific buffer zone.
Backward reference is the simplest and most useful application, which provides the ability to determine the continuous occurrence of two identical words in a text. See the following sentence:
Is it the cost of gasoline going up?
According to the content, the above sentence is obviously prone to repeated words. If there is one way to modify the sentence without looking for the repetition of each word. The following Visual Basic Scripting Edition Regular Expression uses a subexpression to implement this function.
/([A-z] +) 1/gi
The equivalent VBScript expression is:
"([A-z] +) 1"
In this example, the subexpression is each of the parentheses. The captured expression contains one or more letter characters, that is, specified by '[a-z] +. The second part of the regular expression is a reference to the previously captured sub-match, that is, the second occurrence word matched by the append expression. '1' is used to specify the first child match. The word boundary character ensures that only individual words are detected. Otherwise, phrases such as "is issued" or "this is" are incorrectly recognized by this expression.
In the Visual Basic Scripting Edition expression, the global sign ('G') after the regular expression indicates that the expression will be used to search for as many matches as possible in the input string. The case sensitivity is specified by the case sensitivity mark ('I') at the end of the expression. Multiline flag specifies a potential match that may appear at both ends of the line break. For VBScript, you cannot set various tags in expressions, but you must use the RegExp object attribute to explicitly set the tags.
Using the regular expression shown above, the following Visual Basic Scripting Edition code can use sub-matching information to replace two identical words with one in a text string:
Var ss = "Is the cost of gasoline going up ?. ";
Var re =/([a-z] +) 1/gim; // create a regular expression style.
Var rv = ss. replace (re, "$1"); // replace two words with one word.

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.