-------------------Regular ExpressionAll content -----------------------------
Regexp attributes
Global
Ignorecase
Pattern
Regexp Method
Execute -- execute Regular Expression search on the specified string.
Replace -- replace the character searched by the regular expression.
Test
Regexp object
Match
Match attribute
Firstindex
Length
Value
Regexp set
Matches
Submatches
Note:
The regular expression search design mode is throughRegexpObjectPattern.
ExecuteMethod returnsMatchesSet, which containsStringFor each matchedMatchObject. If no match is found,ExecuteWill return nullMatchesSet.
Example:
Function regexptest (patrn, strng) dim RegEx, match, matches 'create variable set RegEx = new Regexp 'create regular expression RegEx. pattern = patrn 'set the search method RegEx. ignorecase = true' indicates whether the RegEx is case sensitive. global = true' set full availability set matches = RegEx. execute (strng) 'executes the search string for each match in matches according to the regular expression rules to traverse the matches set retstr = retstr & match. value & "," 'displays the characters that conform to the regular expression rules. This sentence can also be written as: retstr = retstr & match next regexptest = retstrend function STR = regexptest ("\ D + ", "xxafaf12dfasf3433432xx hello") msgbox STR: 12,3433432
View code
Function RegExpTest(patrn, strng) Dim regEx, Match, Matches Set regEx = New RegExp regEx.Pattern = patrn regEx.IgnoreCase = True regEx.Global = True Set Matches = regEx.Execute(strng) For Each Match in Matches RetStr = RetStr & Match.value Next RegExpTest = RetStrEnd Functionmsgbox RegExpTest("\d+","sdfsdfsd234")
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.
Regexp's replace Method Introduction:
Replace -- Replace the text searched in the regular expression.
Object. Replace (string1, string2)
Parameters:
Object
Required. Always the name ofRegexpObject.
String1
Required.String1Is the text string in which the text replacement is to occur.
String2
Required.String2Is the replacement text string.
Note:
The actual mode of the replaced text is set through the pattern attribute 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, the original copy of string1 is returned.
The following example describes how to use the replace method.
Function ReplaceTest(patrn,replStr) Dim regEx,str1 str1 = “the quick browm fox jumps over the lazy dog.” ‘create regular expression Set regEx = New RegExp regEx.Pattern = patrn regEx.IgnoreCase = True ‘make replacement. ReplaceTest = regEx.Replace(str1,replStr)End FunctionMsgbox (ReplaceTest(“fox”,”cat”)) ‘replace ‘fox’ with ‘cat’.
Regexp Test Method Introduction:
Test Method
Perform a regular expression search on the specified string and return a Boolean value indicating whether the matching mode is found.
Object. Test (string)
Parameters
Object
Required. It is always the name of a Regexp object.
String
Required. The text string to be searched by the regular expression.
Description
The actual pattern of Regular Expression search is set through the pattern attribute of the Regexp object. The Regexp. Global attribute has no effect on the test method.
If the matching mode is found, the test method returns true; otherwise, false.
The following code illustrates the usage of the test method.
Function regexptest (patrn, strng) dim RegEx, retval 'to create a variable. Set RegEx = new Regexp 'to create a regular expression. RegEx. pattern = patrn 'setting mode. RegEx. ignorecase = false' specifies whether to enable case sensitivity. Retval = RegEx. Test (strng. If retval then regexptest = "finds one or more matches. "Else regexptest =" no matching found. "End ifend functionmsgbox (regexptest (" is. "," is1 is2 is3 is4 "))
Submatches set
A set of regular expressions that match strings.
Description
The submatches set contains a single child matching string, which can only be created using the execute method of the Regexp object. The attributes of the submatches set are read-only.
When running a regular expression, when a subexpression is captured in parentheses, there can be zero or multiple child matches. Each item in the submatches set is a string found and captured by a regular expression.
The following code demonstrates how to obtain a submatches set from a regular expression and how to obtain its proprietary members:
Function submatchtest (inpstr) dim ore, omatch, omatches set ORE = new Regexp 'find an email address (not an ideal Regexp) ore. pattern = "(\ W +) @ (\ W + )\. (\ W +) "'get the matches set omatches = ore. execute (inpstr) 'to obtain the first set omatch = omatches (0)' result string in the matches set. The match object is the full match-dragon@xyzzy.com retstr = "the email address is:" & omatch & vbnewline 'gets the child matching part of the address. Retstr = retstr & "the email alias is:" & omatch. submatches (0) 'Dragon retstr = retstr & vbnewline retstr = retstr & "organization:" & omatch. submatches (1) 'xy‑submatchtest = retstrend Function
Msgbox (submatchtest ("please write to dragon@xyzzy.com. Thank you! "))