Detailed usage of the RegExp object

Source: Internet
Author: User
Tags alphabetic character character set numeric regular expression

The RegExp object provides simple regular expression support functionality.

RegExp the use of objects:
Function regexptest (PATRN, STRNG)
Dim regEx, Match, Matches ' build variable.
Set regex = New RegExp ' establishes a regular expression.
Regex.pattern = Patrn ' setup mode.
Regex.ignorecase = True ' Sets whether character case is distinguished.
Regex.global = True ' to set global availability.
Set Matches = Regex.execute (strng) ' performs the search.
The for each match in Matches ' iterates over 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"))

REGEXP properties of an object

Global Properties

The global property sets or returns a Boolean value that indicates whether the pattern matches the entire search string or only the first one.
Grammar
Object. Global [= True | False]
The object parameter is always REGEXP objects. If the search applies to the entire string, the value of the Global property is True, otherwise its value is False. The default setting is True.

Use of the global property (change the value given to the global property and observe its effect):
Function regexptest (PATRN, STRNG)
Dim regEx ' builds variables.
Set regEx = New RegExp ' establishes a canonical expression.
Regex.pattern = Patrn ' setup mode.
Regex.ignorecase = True ' Sets whether the case of letters is distinguished.
Regex.global = True ' Sets the whole nature.
Regexptest = Regex.execute (strng) ' performs the search.
End Function

MsgBox (Regexptest ("is.", "IS1 is2 IS3 is4"))

IgnoreCase Property

The IgnoreCase property sets or returns a Boolean value that indicates whether the pattern search is case-sensitive.
Grammar
Object. IgnoreCase [= True | False]
The object parameter is always a RegExp object. True if the search is case-sensitive, the IgnoreCase property is False; The default value is True.

Use the IgnoreCase property (change the value given to the IgnoreCase property to see its effect):
Function regexptest (PATRN, STRNG)
Dim regEx ' builds variables.
Set regex = New RegExp ' establishes a regular expression.
Regex.pattern = Patrn ' setup mode.
Regex.ignorecase = True ' setting is case-sensitive.
Regexptest = Regex.execute (strng) ' performs the search.
End Function

MsgBox (Regexptest ("is.", "IS1 is2 IS3 is4"))

Pattern Property

The Pattern property sets or returns the regular expression pattern that is searched.
Grammar
Object. Pattern [= "SearchString"]
The syntax of the Pattern property contains the following sections:

Syntax Description:
object is required. Always a REGEXP object variable.
SearchString is optional. The regular string expression to be searched. It may contain various regular expression characters that are set in a partial table.

Set up
Special characters and sequences are used when writing patterns of regular expressions. The following describes the characters and sequences that can be used, and gives an example.
/Mark the next character as a special character or a literal. For example, "n" matches the character "n". "/n" matches the line break. The sequence "//" matches the "/" Match, "/(" and "(" match.
^ matches the start position of the input.
$ matches the end of the input.
* Match the previous character 0 or more times. For example, "zo*" can Match "Z", "Zoo".
+ Match the previous character one or more times. For example, "zo+" can Match "zoo", but does not match "Z".
? Matches the previous character 0 or one time. For example, "A?ve?" Can match "VE" in "Never".
. Matches any character other than the line break.
(pattern) matches the pattern and remembers the match. The matching substring can be used from the Matches collection as the result of Item [0] ... [n] obtained. If you want to match the bracket character (and), 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. Matches 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. Matches at least n times. For example, "o{2,}" does not match "O" in "Bob", but matches 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. Matches at least n times, up to M times. For example, "o{1,3}" matches the top three o in "Fooooood". "o{0,1}" is equivalent to "O?".
[XYZ] a character set. Matches one of the characters in the parentheses. For example, "[ABC]" matches "a" in "plain".
[^XYZ] a negative character set. Matches any character that is not in this parenthesis. For example, "[^ABC]" can match "P" in "plain".
[A-z] denotes a range of characters. Matches any character within the specified range. For example, "[A-z]" matches any lowercase alphabetic character between "a" and "Z".
[^m-z] the negative character range. Matches characters that are not within the specified interval. For example, "[M-z]" matches any character that is not between "M" to "Z".
/b matches the boundary of the word, which is the position between the word and the space. For example, "er/b" matches "er" in "never", but does not match "er" in "verb".
/b matches a non-word boundary. "Ea*r/b" matches "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 the line break character.
/R matches the carriage return character.
/s matches any white-space character, including spaces, tabs, page breaks, and so on. Equivalent to "[/f/n/r/t/v]".
/S matches any non-whitespace character. Equivalent to "[^/f/n/r/t/v]".
/T is matched with a tab.
/V matches the Vertical tab.
/w matches any word character, including underscores. 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. Reference back to the remembered match. For example, "(.) /1 "matches two consecutive identical characters.
The/n matches N, where n is an octal code-break value. The octal code value must be 1, 2, or 3 digits long. For example, "/11" and "/011" are all matched with a tab. "/0011" is equivalent to "/001" and "1". Octal code value must not exceed 256. Otherwise, only the first two characters are considered part of an expression. Allows ASCII code to be used in regular expressions.
/XN matches N, where n is a hexadecimal code-break value. The hexadecimal code value must be exactly two digits long. For example, "/x41" matches "A". "/x041" is equivalent to "/x04" and "1". Allows ASCII code to be used in regular expressions.

Use of the Pattern property:
Function regexptest (PATRN, STRNG)
Dim regEx ' builds variables.
Set regex = New RegExp ' establishes a regular expression.
Regex.pattern = Patrn ' setup mode.
Regex.ignorecase = True ' setting is case-sensitive.
Regexptest = Regex.execute (strng) ' performs the search.
End Function

MsgBox (Regexptest ("is.", "IS1 is2 IS3 is4"))

Methods for RegExp objects

Execute method

The Execute method performs a regular expression search on the specified string.
Grammar
Object. Execute (String)
Syntax Section description
object is required. Always a name for the RegExp object.
string is required. The text string on which to execute the regular expression.

Description
The design pattern of a regular expression search is set by the pattern of the RegExp object.
The Execute method returns a Matches collection that contains each matching match object found in the string. If no match is found, Execute returns an empty Matches collection.

Use of the Execute method:
Function regexptest (PATRN, STRNG)
Dim regEx ' builds variables.
Set regex = New RegExp ' establishes a regular expression.
Regex.pattern = Patrn ' setup mode.
Regex.ignorecase = False ' setting is case sensitive.
Regex.global = True ' Search all matches.
Regexptest = Regex.execute (strng) ' performs 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 Section description
object is required. Always a name for the RegExp object.
String1 is required. String1 is the string that will be replaced with text.
String2 is required. String2 is the replacement text string.

Description
The actual pattern of the replaced text is set by the RegExp object's Pattern property.
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 is returned.

Usage of the Eplace method:
Function replacetest (PATRN, REPLSTR)
Dim regEx, str1 ' build variable.
STR1 = "The quick brown fox jumped over the lazy dog."
Set regex = New RegExp ' establishes a regular expression.
Regex.pattern = Patrn ' setup mode.
Regex.ignorecase = True ' setting is case-sensitive.
Replacetest = Regex.Replace (str1, replstr) ' for substitution.
End Function

MsgBox (Replacetest ("Fox", "Cat"))
' Replace ' fox ' with ' cat '.

In addition, the Replace method replaces subexpressions in the pattern. The following call to the function in the previous example replaces all pairs of words 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 is found.
Grammar
Object. Test (String)
Syntax Section description
object is required. Always a name for the RegExp object.
string is required. The text string to perform the regular expression search.

Description
The actual pattern of regular expression searches is set by 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 false is returned.

Use of the Test method:
Function regexptest (PATRN, STRNG)
Dim regEx, retVal ' build variable.
Set regex = New RegExp ' establishes a regular expression.
Regex.pattern = Patrn ' setup mode.
Regex.ignorecase = False ' setting is case-sensitive.
RetVal = regex.test (strng) ' performs a search test.
If RetVal Then
Regexptest = "found one or more matches. "
Else
Regexptest = "no match found. "
End If
End Function

MsgBox (Regexptest ("is.", "IS1 is2 IS3 is4"))

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.