Function usage of RegExp object Regular Expression in asp [comprehensive]

Source: Internet
Author: User

RegExp objects support simple regular expressions.
RegExp object usage:
Copy codeThe Code is as follows:
Function RegExpTest (patrn, strng)
Dim regEx, Match, Matches 'to create a variable.
Set regEx = New RegExp 'to create a regular expression.
RegEx. Pattern = patrn 'setting mode.
RegEx. IgnoreCase = true' specifies whether the characters are case sensitive.
RegEx. Global = true' to set Global availability.
Set Matches = regEx. Execute (strng) 'to Execute the search.
For Each Match in Matches 'traverses the matching set.
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 Object Attributes
◎ Global attributes
The Global attribute is set or a Boolean value is returned, indicating whether the pattern matches all or only the first character in the entire search string.
Syntax
Object. Global [= True | False]
The object parameter is always a RegExp object. If the search is applied to the entire string, the value of the Global attribute is True; otherwise, the value is False. The default value is True.
Usage of the Global attribute (change the value assigned to the Global attribute and observe its effect ):
Copy codeThe Code is as follows:
Function RegExpTest (patrn, strng)
Dim regex' creates a variable.
Set regEx = New RegExp 'to create a regular expression.
RegEx. Pattern = patrn 'setting mode.
RegEx. IgnoreCase = true' specifies whether to distinguish uppercase and lowercase letters.
RegEx. Global = true' sets the full nature.
RegExpTest = regEx. Execute (strng.
End Function
MsgBox (RegExpTest ("is.", "IS1 is2 IS3 is4 "))

◎ IgnoreCase attributes
Set the IgnoreCase attribute or return a Boolean value to indicate whether the mode search is case sensitive.
Syntax
Object. IgnoreCase [= True | False]
The object parameter is always a RegExp object. If the search is case sensitive, the IgnoreCase attribute is False; otherwise, the value is True. The default value is True.
Usage of the IgnoreCase attribute (change the value assigned to the IgnoreCase attribute to observe its effect ):
Copy codeThe Code is as follows:
Function RegExpTest (patrn, strng)
Dim regex' creates a variable.
Set regEx = New RegExp 'to create a regular expression.
RegEx. Pattern = patrn 'setting mode.
RegEx. IgnoreCase = true' is used to set Case sensitivity.
RegExpTest = regEx. Execute (strng.
End Function

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

◎ Pattern attributes
Set the Pattern attribute or return the regular expression Pattern to be searched.
Syntax
Object. Pattern [= "searchstring"]
The syntax of the Pattern attribute includes the following parts:
Syntax description:
Required. Always a RegExp object variable.
Searchstring is optional. The regular string expression to be searched. It may contain various regular expression characters in some tables.
Set
Special characters and sequences are used when writing regular expressions. The following describes the characters and sequences that can be used and provides examples.
Mark the next character as a special character or literal value. For example, "n" matches the character "n. "N" matches the linefeed. The sequence "\" matches with "", and "(" matches.
^ Match the start position of the input.
$ Matches the end of the input.
* Matches the first character Zero or several times. For example, "zo *" can match "z" and "zoo ".
+ Match the previous character once or multiple times. For example, "zo +" can match "zoo" but does not match "z ".
? Match the first character Zero or once. For example, "? Ve? "Matches" ve "in" never ".
. Match any character other than the line break.
(Pattern) matches the pattern and remembers the matching. The matched substring can be obtained from the Matches set used as the result using Item [0]... [n. To match the parentheses (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 it 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 the brackets. For example, "[abc]" matches "a" in "plain ".
[^ Xyz] A negative character set. Match any character that does not exist in this bracket. For example, "[^ abc]" can match "p" in "plain ".
[A-z] indicates characters in a certain range. Matches any character in the specified range. For example, "[a-z]" matches any lowercase letter between "a" and "z.
[^ M-z] The negative character range. Matches a character that is not in the specified range. For example, "[m-z]" matches any character that is not between "m" and "z.
B matches the boundary of a word, that is, the position between a word and a space. For example, "erb" matches "er" in "never", but does not match "er" in "verb ".
B matches the non-word boundary. "Ea * rB" matches "ear" in "never early.
D matches a digit. It is equivalent to [0-9].
D matches non-numeric characters. It is equivalent to [^ 0-9].
F matches the paging character.
N matches the linefeed character.
R matches the carriage return character.
S matches any white characters, including spaces, tabs, and pagination characters. It is equivalent to "[fnrtv]".
S matches any non-blank characters. It is equivalent to "[^ fnrtv]".
T matches the tab.
V matches the vertical tab.
W matches any word characters, including underscores. It is equivalent to "[A-Za-z0-9 _]".
W matches any non-word characters. It is 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.
N matches n, where n is an octal value. The octal value must be 1, 2, or 3 characters long. For example, both "11" and "11" match a tab. "011" is equivalent to "01" and "1 ". The octal value cannot exceed 256. Otherwise, only the first two characters are considered part of the expression. ASCII code can be used in regular expressions.
Xn matches n, where n is a hexadecimal value. The hexadecimal value must be exactly two digits long. For example, "x41" matches "". "X041" is equivalent to "x04" and "1 ". ASCII code can be used in regular expressions.
Usage of the Pattern attribute:
Copy codeThe Code is as follows:
Function RegExpTest (patrn, strng)
Dim regex' creates a variable.
Set regEx = New RegExp 'to create a regular expression.
RegEx. Pattern = patrn 'setting mode.
RegEx. IgnoreCase = true' is used to set Case sensitivity.
RegExpTest = regEx. Execute (strng.
End Function

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

RegExp object Method
◎ Execute Method
Execute method performs regular expression search on the specified string.
Syntax
Object. Execute (string)
Syntax description
Required. It is always the name of a RegExp object.
String is required. The text string on which the regular expression is to be executed.
Description
The regular expression search design mode is set through the Pattern of the RegExp object.
The Execute method returns a Matches set, which contains each matching Match object found in the string. If no match is found, Execute returns an empty Matches set.
Execute method usage:
Copy codeThe Code is as follows:
Function RegExpTest (patrn, strng)
Dim regex' creates a variable.
Set regEx = New RegExp 'to create a regular expression.
RegEx. Pattern = patrn 'setting mode.
RegEx. IgnoreCase = false' is case sensitive.
RegEx. Global = true' search all matches.
RegExpTest = regEx. Execute (strng.
End Function
MsgBox (RegExpTest ("is.", "IS1 is2 IS3 is4 "))

◎ Replace Method
Replace replaces the text found in regular expression search.
Syntax
Object. Replace (string1, string2)
Syntax description
Required. It is always the name of a RegExp object.
Required by string1. String1 is the string to be replaced by text.
Required by string2. String2 is a replacement text string.
Description
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.
Usage of the eplace method:
Copy codeThe Code is as follows:
Function ReplaceTest (patrn, replStr)
Dim regEx, str1' create a variable.
Str1 = "The quick brown fox jumped over the lazy dog ."
Set regEx = New RegExp 'to create a regular expression.
RegEx. Pattern = patrn 'setting mode.
RegEx. IgnoreCase = true' is used to set Case sensitivity.
ReplaceTest = regEx. Replace (str1, replStr) 'to Replace.
End Function
MsgBox (ReplaceTest ("fox", "cat "))
'Replace 'fox' with 'cat '.

In addition, the Replace method replaces subexpressions in the mode. Next, replace all the word pairs in the original string with the calls to functions in the previous example:
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 the matching mode is found.
Syntax
Object. Test (string)
Syntax description
Required. It is always the name of a RegExp object.
String is 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.
Copy codeThe Code is as follows:
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 = "one or more matches are found. "
Else
RegExpTest = "no matching 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.