Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

A detailed introduction to backward references to VBS regular expressions

2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the knowledge of "detailed introduction to the backward citation of VBS regular expressions". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Backward reference

One of the most important features of regular expressions is the ability to store a portion of a successfully matched pattern for later use. Recall that adding parentheses to both sides of a regular expression pattern or partial pattern will cause that part of the expression to be stored in a temporary buffer. You can use non-capture metacharacters'?:','? =', or'?!' To ignore the saving of this part of the regular expression.

Each submatch captured is stored according to what is encountered from left to right in the regular expression pattern. The buffer number of the storage sub-match starts at 1 and is numbered up to a maximum of 99 subexpressions. Each buffer can be accessed using'\ n', where n is an one-or two-digit decimal number that identifies a particular buffer.

One of the simplest and most useful applications of backward reference is the ability to determine the location of two consecutive words in the text. Please look at the following sentence:

Is is the cost of of gasoline going up up?

According to what is written, the above sentence obviously has the problem of repeated words. If only there was a way to modify the sentence without looking for the repetition of each word. The following Visual Basic Scripting Edition regular expression can do this using a subexpression.

/\ b ([amerz] +)\ 1\ b/gi

The equivalent VBScript expression is:

"\ b ([amerz] +)\ 1\ b"

In this example, the subexpression is each item between the parentheses. The captured expression consists of one or more alphabetic characters specified by'[amurz] +'. The second part of the regular expression is a reference to the previously captured sub-match, that is, the second occurrence of the word matched by the additional expression.' \ 1' is used to specify the first child match. Word boundary metacharacters ensure that only individual words are detected. If not, phrases such as "is issued" or "this is" will be incorrectly recognized by the expression.

In a Visual Basic Scripting Edition expression, the global flag ('g') after 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 marker ('i') at the end of the expression. Multiline tags specify potential matches that may occur at both ends of a newline character. For VBScript, you cannot set various tags in an expression, but you must set them explicitly using the properties of the RegExp object.

Using the regular expression shown above, the following Visual Basic Scripting Edition code can use sub-match information to replace the same word that appears twice in a text string with the same word:

Var ss = "Is is the cost of of gasoline going up up?.\ n"; var re = /\ b ([amurz] +)\ 1\ bUniGim; / / create regular expression style .var rv = ss.replace (re, "$1"); / / replace two words with one word.

The closest equivalent VBScript code is as follows:

Dim ss, re, rvss = "Is is the cost of of gasoline going up up?." & vbNewLineSet re = New RegExpre.Pattern = "\ b ([Amurz] +)\ 1\ b" re.Global = Truere.IgnoreCase = Truere.MultiLine = Truerv = re.Replace (ss, "$1")

Note that in VBScript code, global, case sensitivity, and multiline tags are all set using the appropriate properties of the RegExp object.

Use $1 in the replace method to refer to the first saved submatch. If there are multiple submatches, you can continue to quote with $2, $3, and so on.

Another use of backward references is to break down a common resource indicator (URI) into component parts. Suppose you want to decompose the following URI into protocols (ftp, http, etc), domain name addresses, and pages / paths:

Http://msdn.microsoft.com:80/scripting/default.htm

The following regular expression provides this functionality. For Visual Basic Scripting Edition, it is:

/ (\ w +):\ / ([^ /:] +) (:\ d*)? ([^ #] *) /

For VBScript, it is:

"(\ w +):\ / ([^ /:] +) (:\ d*)? ([^ #] *)"

The first additional subexpression is the part of the protocol used to capture the web address. This subexpression matches any word that comes before a colon and two forward slashes. The second additional subexpression captures the domain name address of the address. The subexpression matches any sequence of characters that does not include the'^','/', or': 'characters. The third additional subexpression captures the site port number if the port number is specified. The subexpression matches zero or more numbers followed by a colon. Finally, the fourth additional subexpression captures the path and\ or page information specified by the web address. The subexpression matches one and more characters except'#'or spaces.

After applying the regular expression to the URI shown above, the submatch contains the following:

RegExp.$1 contains "http"

RegExp.$2 contains "msdn.microsoft.com"

RegExp.$3 contains ": 80"

RegExp.$4 contains "/ scripting/default.htm"

This is the end of the "detailed introduction to references to VBS regular expressions". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 291

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report