Recommended: Asp regular expression learning We can easily verify the legality of various data by "regular expression" object. First, let's understand what exactly is the "regular expression" object of VBScript. Let's first look at a program:
RegExp objects provide simple regular expression support capabilities.
Usage of RegExp object:
| The following is the quoted content: Function RegExpTest(patrn, strng) Dim regEx, Match, Matches ' Create variables. Set regEx = New RegExp ' Create regular expression. regEx.Pattern = patrn ' Set mode. regEx.IgnoreCase = True ' Set whether characters are case sensitive. regEx.Global = True ' Set global availability. Set Matches = regEx.Execute(strng) ' Perform the search. For Each Match in Matches ' traverses the matching collection. RetStr = RetStr & Match found at position RetStr = RetStr & Match.FirstIndex & . Match Value is ' RetStr = RetStr & Match.Value & '. & vbCRLF Next RegExpTest = RetStr End Function |
MsgBox(RegExpTest(is., IS1 is2 IS3 is4))
Properties of RegExp Object
◎ Global attributes
The Global property sets or returns a Boolean value that indicates whether the pattern matches all or only the first one during the entire search string.
grammar
object.Global [= True | False ]
The object parameter is always a RegExp object. If the search is applied to the entire string, the Global property has a value of True, otherwise its value is False. The default setting is True.
Usage of Global attributes (change the value assigned to the Global attribute and observe its effect):
| The following is the quoted content: Function RegExpTest(patrn, strng) Dim regEx ' Create variable. Set regEx = New RegExp ' Create canonical expression. regEx.Pattern = patrn ' Set mode. regEx.IgnoreCase = True ' Set whether letters are case sensitive. regEx.Global = True ' Set the full nature. RegExpTest = regEx.Execute(strng) ' Perform the search. End Function |
MsgBox(RegExpTest(is., IS1 is2 IS3 is4))
◎ IgnoreCase attribute
The IgnoreCase property sets or returns a Boolean value indicating whether the mode search is case sensitive.
grammar
object.IgnoreCase [= True | False ]
The object parameter is always a RegExp object. If the search is case sensitive, the IgnoreCase property is False; otherwise, True. The default value is True.
Usage of the IgnoreCase attribute (change the value assigned to the IgnoreCase attribute to observe its effect):
| The following is the quoted content: Function RegExpTest(patrn, strng) Dim regEx ' Create variable. Set regEx = New RegExp ' Create regular expression. regEx.Pattern = patrn ' Set mode. regEx.IgnoreCase = True ' Set whether it is case sensitive. RegExpTest = regEx.Execute(strng) ' Perform the search. End Function |
MsgBox(RegExpTest(is., IS1 is2 IS3 is4))
◎ Pattern attributes
The Pattern property sets or returns the regular expression pattern being searched.
grammar
object.Pattern [= searchstring]
The syntax of the Pattern property contains the following parts:
Syntax description:
object Required. Always a RegExp object variable.
searchstring optional. The regular string expression being searched. It may contain various regular expression characters set in a partial table.
set up
Special characters and sequences are used when writing patterns of regular expressions. The characters and sequences that can be used are described below and examples are given.
Mark the next character as a special character or literal value. For example n matches the character n. n matches line breaks. Sequence/match opposite, (match (match.
^ Match the start position of the input.
$ Match the end of the input.
* Match the previous character zero or several times. For example, zo* can match z and zoo.
Match the previous character once or more times. For example, zo can match zoo, but not z.
? Match the previous character zero or once. For example, a?ve? can match ve in never.
. Match any character other than line breaks.
(pattern) Match the pattern and remember the match. The matching substring can be obtained from the resulting Matches collection using Item [0]...[n]. If you want to match the bracket characters (and), you can use (or).
x|y matches x or y. For example, z|food can match z or food. (z|f)ood matches zoo or food.
{n} n is a non-negative integer. Match exactly n times. For example, o{2} cannot match o in Bob, but can match the first two o in Foooood.
{n,} n is a non-negative integer. Match at least n times. For example, o{2,} does not match o in Bob, but all o in Foooood. o{1,} is equivalent to o . o{0,} is equivalent to o*.
{n,m} m and n are non-negative integers. Match at least n times, up to m times. For example, o{1,3} matches the first three os in fooooood. o{0,1} is equivalent to o?.
[xyz] A character set. Matches one of the characters in brackets. For example, [abc] matches a in plain.
[^xyz] A negative character set. Match any characters not in this bracket. For example, [^abc] can match p in plain.
[az] represents a character in a certain range. Matches any character within the specified interval. For example, [az] matches any lowercase alphabetical character between a and z.
[^mz] Negative character interval. Matches characters that are not within the specified interval. For example, [mz] matches any character that is not between m and z.
b matches the boundary of the word, that is, the position between the word and the space. For example, erb matches er in never, but does not match er in verb.
B matches non-word boundaries. ea*rB matches the ear in never early.
d Matches a numeric character. Equivalent to [0-9].
D matches non-numeric characters. Equivalent to [^0-9].
f matches the page break.
n Matches line break character.
r matches the carriage return character.
s matches any white character, including spaces, tabs, page breaks, etc. Equivalent to [fnrtv].
S matches any non-blank character. Equivalent to [^ fnrtv].
t matches the tab character.
v Matches vertical tab characters.
w Matches any word character, including underscore. Equivalent to [A-Za-z0-9_].
W matches any non-word character. Equivalent to [^A-Za-z0-9_].
num matches num, where num is a positive integer. Quote back to remember matches. For example, (.)1 matches two consecutive identical characters.
n matches n, where n is an octal code transfer value. The octal code transfer value must be 1, 2 or 3 numbers long. For example, both 11 and 11 match a tab. 011 is equivalent to 01 and 1. The octal code transfer value shall not exceed 256. Otherwise, only the first two characters are considered part of the expression. Allows the use of ASCII code in regular expressions.
xn matches n, where n is a hexadecimal code transfer value. The hexadecimal code transfer value must be exactly two numbers long. For example, x41 matches A. x041 is equivalent to x04 and 1. Allows the use of ASCII code in regular expressions.
Usage of Pattern attribute:
| The following is the quoted content: Function RegExpTest(patrn, strng) Dim regEx ' Create variable. Set regEx = New RegExp ' Create regular expression. regEx.Pattern = patrn ' Set mode. regEx.IgnoreCase = True ' Set whether it is case sensitive. RegExpTest = regEx.Execute(strng) ' Perform the search. End Function |
MsgBox(RegExpTest(is., IS1 is2 IS3 is4))
Methods of RegExp Objects
◎ Execute method
The Execute method performs a regular expression search on the specified string.
grammar
object.Execute(string)
Syntax Part Description
object Required. Always the name of a RegExp object.
string Required. The text string on which the regular expression is to be executed.
illustrate
The design pattern for regular expression search is set through the pattern of the RegExp object.
The Execute method returns a Matches collection containing each matching Match object found in the string. If no match is found, Execute returns an empty Matches collection.
Use of Execute method:
| The following is the quoted content: Function RegExpTest(patrn, strng) Dim regEx ' Create variable. Set regEx = New RegExp ' Create regular expression. regEx.Pattern = patrn ' Set mode. regEx.IgnoreCase = False ' Set case sensitive. regEx.Global = True ' Search for all matches. RegExpTest = regEx.Execute(strng) ' Perform the search. End Function |
MsgBox(RegExpTest(is., IS1 is2 IS3 is4))
◎ Replace method
The Replace method replaces the text found in the regular expression lookup.
grammar
object.Replace(string1, string2)
Syntax Part Description
object Required. Always the name of a RegExp object.
string1 Required. string1 is the string to which text replacement is to be performed.
string2 Required. string2 is a replacement text string.
illustrate
The actual pattern of the replaced text is set through the Pattern property of the RegExp object.
The Replace method returns a copy of string1, where the RegExp.Pattern text has been replaced with string2. If no matching text is found, a copy of the original string1 will be returned.
Usage of the eplace method:
| The following is the quoted content: Function ReplaceTest(patrn, replStr) Dim regEx, str1 ' Creates a variable. str1 = The quick brown fox jumped over the lazy dog. Set regEx = New RegExp ' Create regular expression. regEx.Pattern = patrn ' Set mode. regEx.IgnoreCase = True ' Set whether it is case sensitive. ReplaceTest = regEx.Replace(str1, replStr) ' as a replacement. End Function |
MsgBox(ReplaceTest(fox, cat))
'Replace 'fox' with 'cat'.
; In addition, the Replace method replaces subexpressions in the pattern. The following calls to the function in the previous example replace all word pairs in the original string:
MsgBox(ReplaceText((S )(s )(S ), $3$2$1)) ' Swap pairs of words.
◎ Test method
The Test method performs a regular expression search on the specified string and returns a Boolean value indicating whether a matching pattern was found.
grammar
object.Test(string)
Syntax Part Description
object Required. Always the name of a RegExp object.
string Required. The text string to perform a regular expression search.
illustrate
The actual pattern of regular expression search is set through the Pattern property of the RegExp object. The RegExp.Global property has no effect on the Test method.
If a matching pattern is found, the Test method returns True; otherwise, it returns False.
| The following is the quoted content: Usage of Test method: Function RegExpTest(patrn, strng) Dim regEx, retVal ' Create variable. Set regEx = New RegExp ' Create regular expression. regEx.Pattern = patrn ' Set mode. regEx.IgnoreCase = False ' Set whether it is case sensitive. retVal = regEx.Test(strng) ' Perform a search test. If retVal Then RegExpTest = Find one or more matches. Else RegExpTest = No match found. End If End Function |
MsgBox(RegExpTest(is., IS1 is2 IS3 is4))
Share: How to output Excel files with ASP One way to use excel in Asp is to link the excel file as a database, and the operation is similar to the access database operation. But this method is not always useful, it should be that excel is not a relational database. For a fixed format,