ASP RegExp object Regular expression functional usage [compare full]_ Regular expression

Source: Internet
Author: User
Tags numeric regular expression
The RegExp object provides a simple regular expression support feature.
Use of RegExp objects:
Copy Code code as follows:

Function regexptest (PATRN, STRNG)
Dim regEx, Match, matches ' Set variable.
Set regEx = New RegExp ' establishes a regular expression.
Regex.pattern = Patrn ' Set mode.
Regex.ignorecase = True ' Sets whether character case is case-sensitive.
Regex.global = True ' Sets global availability.
Set matches = Regex.execute (strng) ' performs a search.
For the match in matches ' traversal 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 the RegExp object
Global property
The global property sets or returns a Boolean value that indicates whether the pattern matches all or only the first of the entire search string.
Grammar
Object. Global [= True | False]
The object argument 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 assigned to the global property and observe its effect):
Copy Code code as follows:

Function regexptest (PATRN, STRNG)
Dim regEx ' establishes a variable.
Set regEx = New RegExp ' establishes a canonical expression.
Regex.pattern = Patrn ' Set mode.
Regex.ignorecase = True ' Sets whether the case of letters is case-sensitive.
Regex.global = True ' Sets the whole property.
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 argument is always a RegExp object. If the search is case-sensitive, the IgnoreCase property is False; otherwise, true. The default value is True.
Use of the IgnoreCase property (change the value given to the IgnoreCase property to observe its effect):
Copy Code code as follows:

Function regexptest (PATRN, STRNG)
Dim regEx ' establishes a variable.
Set regEx = New RegExp ' establishes a regular expression.
Regex.pattern = Patrn ' Set mode.
Regex.ignorecase = True ' Sets whether case sensitive.
Regexptest = Regex.execute (strng) ' performs the search.
End Function

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

Pattern Properties
The Pattern property sets or returns the regular expression patterns that are searched.
Grammar
Object. pattern [= "searchstring"]
The syntax of the pattern attribute consists of the following sections:
Syntax Description:
Required for object. Always a REGEXP object variable.
SearchString is optional. The regular string expression being searched. It may contain various regular expression characters in the Set section table.
Set up
Special characters and sequences are used when writing patterns for regular expressions. The following describes the characters and sequences that can be used, and gives an example.
Marks the next character as a special character or literal. For example, "n" matches the character "n". "N" matches the line feed character. The sequence "\" and "" Match opposite, "(" and "() match.
^ matches the start position of the input.
$ matches the end of the input.
* Match the previous character 0 times or several 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 more times. For example, "A?ve?" Can match "never" in the "VE".
. Matches any character other than the newline character.
(pattern) matches patterns and remembers matches. The matching substring can be used with the Item [0] from the matches collection as the result ... [n] obtained. If you want to match 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. 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. Match 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. Match at least n times, at most m times. For example, "o{1,3}" matches the first three o in "Fooooood". "o{0,1}" is equivalent to "O?".
[XYZ] a character set. Matches one of the characters in parentheses. For example, "[ABC]" matches "a" in "plain".
[^XYZ] a negative character set. Matches any character that is not in this bracket. For example, "[^ABC]" can match "P" in "plain".
[A-z] denotes a range of characters. Matches any character within the specified interval. For example, "[A-z]" matches any of the lowercase alphabetic characters between "a" and "Z".
[^m-z] The character interval of the negation. Matches characters that are not in the specified interval. For example, "[M-z]" matches any character that is not between "M" and "Z".
b matches the boundary of the word, which 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 a non word boundary. "EA*RB" matches the "ear" in "Never early".
D matches a numeric character. equivalent to [0-9].
D matches characters that are not numeric. equivalent to [^0-9].
F matches page breaks.
n matches the line break character.
R matches the carriage return character.
s matches any white character, including spaces, tabs, page breaks, and so on. Equivalent to "[Fnrtv]".
S matches any character that is not blank. Equivalent to "[^ Fnrtv]".
T matches the tab character.
V matches the Vertical tab.
W matches any word characters, 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. References are returned to the remembered match. For example, "(.) 1 "matches two consecutive identical characters.
n matches N, where n is a octal value. The octal change value must be 1, 2 or 3 digits long. For example, "11" and "11" All match a tab. "011" is equivalent to "01" and "1". The value of the octal converter cannot 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-changing value. The hexadecimal transposition 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.
Usage of pattern attributes:
Copy Code code as follows:

Function regexptest (PATRN, STRNG)
Dim regEx ' establishes a variable.
Set regEx = New RegExp ' establishes a regular expression.
Regex.pattern = Patrn ' Set mode.
Regex.ignorecase = True ' Sets whether case sensitive.
Regexptest = Regex.execute (strng) ' performs 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)
The grammar section describes
Required for object. Always the name of a 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 RegExp object patterns.
The Execute method returns a matches collection that contains each matching match object found in string. If no match is found, Execute returns an empty matches collection.
Use of the Execute method:
Copy Code code as follows:

Function regexptest (PATRN, STRNG)
Dim regEx ' establishes a variable.
Set regEx = New RegExp ' establishes a regular expression.
Regex.pattern = Patrn ' Set 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)
The grammar section describes
Required for object. Always the name of a RegExp object.
String1 is required. String1 is the string that will be replaced with text.
String2 is required. String2 is a replacement text string.
Description
The actual mode of the replaced text is set by the RegExp object's Pattern property.
The Replace method returns a copy of the 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.
Use of the Eplace method:
Copy Code code as follows:

Function replacetest (PATRN, REPLSTR)
Dim regEx, str1 ' Set variable.
STR1 = "The quick brown fox jumped over the lazy dog."
Set regEx = New RegExp ' establishes a regular expression.
Regex.pattern = Patrn ' Set mode.
Regex.ignorecase = True ' Sets whether case sensitive.
Replacetest = Regex.Replace (str1, Replstr) ' for replacement.
End Function
MsgBox (Replacetest ("Fox", "Cat"))
' Replace ' fox ' with ' cat '.

; In addition, the Replace method replaces subexpressions in the pattern. The following is a call to a function in the previous example that replaces all the 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 indicating whether the matching pattern is found.
Grammar
Object. Test (String)
The grammar section describes
Required for object. Always the name of a RegExp object.
string is required. The text string to perform a regular expression search for.
Description
The actual mode of the regular expression search is set by regexp the Pattern property of the object. The Regexp.global property has no effect on the test method.
The test method returns true if a matching pattern is found, otherwise it returns false.
Copy Code code as follows:
Test Method Usage:
Function regexptest (patrn, strng)
Dim regEx, RetVal ' Create a variable. The
Set regEx = New RegExp ' establishes a regular expression.
Regex.pattern = Patrn ' Set mode. The
Regex.ignorecase = False ' setting is case-sensitive.
RetVal = regex.test (strng) ' performs a search test.
If retVal Then
Regexptest = "Find one or more matches. '
Else
Regexptest = ' did not find a match. '
End-If
End Function
MsgBox (Regexptest (' Is. ', ' IS1 is2 IS3 is4 '))
Related Article

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.