Back reference
One of the most important features of regular expressions is the ability to store portions of a successful pattern for later use. Recall that adding parentheses around a regular expression pattern or part of a pattern causes this part of the expression to be stored in a temporary buffer. You can use a non-capture meta character '?: ', '? = ', or '?! ' to ignore the preservation of this part of the regular expression.
Each captured child match is stored in the content that is encountered from left to right in the regular expression pattern. The buffer number for the storage child match starts at 1 and is numbered consecutively up to 99 subexpression. Each buffer can use ' access, where n is a single or two-bit decimal number that identifies a particular buffer.
The simplest and most useful application of the post reference is to provide the ability to determine the position of two consecutive words in the text. Take a look at the following sentence:
Gasoline going up?
According to the written content, the above sentence obviously has the problem that the word repeats many times. It would be nice if there was a way to modify the sentence without having to look up the repetition of each word. The following Visual Basic scripting Edition Regular expressions use a subexpression to implement this functionality.
/([a-z]+) 1/gi
The equivalent VBScript expression is:
"([a-z]+) 1"
In this example, the subexpression is each item between the parentheses. The captured expression includes one or more alphabetic characters, which are specified by ' [a-z]+ '. The second part of the regular expression is a reference to the previously captured substring, the second occurrence of a word that is matched by an additional expression. ' 1 ' is used to specify the first child match. The word boundary Meta character ensures that only individual words are detected. If not, phrases such as "is issued" or "This is" are incorrectly recognized by the expression.
In the Visual Basic scripting Edition expression, the global flag (' G ') following the regular expression indicates that the expression will be used to find as many matches as possible in the input string. Case sensitivity is specified by the case sensitivity mark (' I ') at the end of the expression. A multiline tag specifies a potential match that may appear at both ends of a newline character. For VBScript, various tags cannot be set in an expression, but must be explicitly set using the properties of the RegExp object.
Using the regular expression shown above, the following Visual Basic scripting Edition code can use the child matching information to replace the same word in a literal string with a two consecutive occurrences of the same word:
var ss = ' is ' The cost of ' gasoline going up? ';
var re =/([a-z]+) 1/gim; Creates a regular expression style.
var rv = Ss.replace (Re, "$"); Replace two words with one word.
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.