Detailed explanation of the regular expression syntax in VBScript

Source: Internet
Author: User
Document directory
  • Detailed explanation of the regular expression syntax in VBScript
★● Blackbase Forum ●★'S archiver ★● Blackbase Forum ●★» Software development technology» detailed explanation of the regular expression syntax in VBScript

330466386Published

Detailed explanation of the regular expression syntax in VBScript

In VBScript, the most powerful and flexible expressions are Regexp objects and classes.

It was not long ago that I fully understood regular expressions.

----------------- All contents of the regular expression -----------------------------

Regexp attributes
Global
Ignorecase
Pattern

Regexp Method
Execute
Replace
Test

Regexp object
Match
Match attribute
Firstindex
Length
Value

Regexp set
Matches
Submatches

------------------ These contents are included in the VBScript manual of the top post -----------------------------

When I learned a regular expression, I did not know what a regular expression was, and my understanding of it was so vague that I had been familiar with it for a long time, although the vbs manual had a very detailed explanation.

A regular expression is a column action that is used to search text and then process the search results.

Let's take a look at a well-known UBB code.
[Quote] function UBB (STR)
Dim Rex, match, matches & #39; create a variable.
Set REX = new Regexp & #39; create a regular expression.
Rex. ignorecase = true & #39; Specifies whether letters are case sensitive.
Rex. Global = true & #39; set the full nature.
Rex. pattern = "/[url = (. *)] (. *)/[// URL]" & amp; #39; set the mode. The official explanation is as follows. I think we should use another method: the search method, so that we can understand it easily.
UBB = Rex. Replace (STR, "<a href = $1> $2 </a>") & #39; replace the searched content
& #39; we used two parentheses when setting the search. That is to say, we only need to use the content in these two parentheses, the content in these two parentheses will be replaced by $1 (content in the first bracket) and $2 (content in the second bracket.
End Function
& #39; usage
Dim text
TEXT = "[url = http://www.aaa.com] link [/url]"
Document. Write UBB (text) & #39; output the text and call the Regular Expression
& #39; if used in ASP, replace the document with response [/quote]
The above is a simple regular expression demonstration. In this demonstration, we only use one line of content, replace only processes the first UBB Code header and the end of the last UBB code in a row when replacing text, as shown below:
[Url = http://www.aaa.com] link [/url] [url = http://www.aaa.com] link [/url]
Will be replaced with <a href = http://www.aaa.com] link [/url] [url = http://www.aaa.com> </a>, this result is not what we want,
That is to say, we must process all the text or strings to be searched before replacing a large amount of search content. The processing result is to ensure that only one corresponding code exists in each line. This is very important. I have never understood why when I study? Later I learned why.

In the previous example, we used regular expressions to make all attributes and replace methods. Next, let's take a look at an official example of the method execute, object match, and all attributes, a collection of matches.
[Quote] function regexptest (patrn, strng)
Dim RegEx, match, matches & #39; create a variable.
Set RegEx = new Regexp & #39; create a regular expression.
RegEx. pattern = patrn & #39; set the mode. That is, the Search Method
RegEx. ignorecase = true & #39; set Case sensitivity.
RegEx. Global = true & #39; set full availability.
Set matches = RegEx. Execute (strng) & #39; execute the search. We can understand it as a string to be searched.
For each match in matches & #39; traverse the matches set.
Retstr = retstr & "match" & Match & #39; note: this is an error in the official examples,
Here is the matched name
Retstr = retstr & "located in" & Match. firstindex &". "& #39; this is the first character of the matched content. The number starts from 0.
Retstr = retstr & "the matched length is" & Match. Length & "characters. "Do not explain it here?
Retstr = retstr & vbcrlf & #39; line feed
Next
Regexptest = retstr
End Function
Document. Write (regexptest ("is.", "is1 is2 is3 is4 "))
& #39; if used in ASP, replace the document with response [/quote]
In this example, we can see that the method execute and object match are used in the matches set. Match and matches are used as variables, so that we can easily understand it, we do not need to change their names. I have commented on the properties of object match in the above example.

Finally, let's take a look at the submatches set. The Code is as follows: (because the official code is vague, I modified it)
[Quote] function submatchtest (inpstr)
Dim re, match, matches
Set Re = new Regexp
Re. pattern = "(/W +) @ (/W +)/. (/W + )"
Set matches = Re. Execute (inpstr)
Set match = matches (0) & #39; because no loop is used here, you can only search once. Only match the first searched content
Retstr = "email address:" & Match & vbnewline & #39; here the Matching content
Retstr = retstr & "the email alias is:" & Match. submatches (0) & #39; Content in the first bracket
Retstr = retstr & vbnewline
Retstr = retstr & "organization:" & Match. submatches (1) & #39; Content in the second bracket
Submatchtest = retstr
End Function
Document. Write (submatchtest ("please write to the dragon@xyzzy.com. Thank you! "))
& #39; if used in ASP, replace the document with response [/quote]
In the above code, we can see another usage of object match and set matches. It can be seen that all sets can use this method, just like a practical array. After reading the code above, some friends may think that since the $1 $2... $ n method has been provided in the attribute pattern, why does the set submatches still exist? In fact, when we use a regular expression, we may search for the content or string to be matched for a long time. However, we need to further process the searched string, and $ cannot implement this, so we have a set of submatches.

The content of VBScript's regular expression is all finished, the above three codes can be used in the <script language = "VBScript"> </SCRIPT> label or in ASP for execution.

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.