Common ASP Regular Expressions

Source: Internet
Author: User

Regexp is used to create a regular expression.
For example:
Set RegEx = new Regexp

RegEx. pattern is used to set the regular expression mode,
For example:
RegEx. pattern = "/d +"

RegEx. ignorecase = true' specifies whether the value is case sensitive.
RegEx. Global = true' to set full availability.

Regexp has three methods: Execute, test, and replace.

The test method performs a regular expression search on the specified string and returns a Boolean value indicating whether the matching mode is found. The Regexp. Global attribute has no effect on the test method. If the matching mode is found, the test method returns true; otherwise, false.
Example:

During the test, msgbox is used by vbs. If it is an ASP file, replace msgbox with response. Write
Copy the Code as follows:
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 ("/d +", "abcd1234 "))
Msgbox (regexptest ("/d +", "ABCD "))

Replace method replaces the text found in regular expression search
Example:
Vbs code
Copy the Code as follows:
Function replacetest (STR, patrn, replstr)
Dim RegEx, str1' create a variable.
'Str1 = "Dog 123 ."
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 (STR, replstr) 'to replace.
End Function

Msgbox (replacetest ("Dog 123", "/d +", "cat") 'replaces 123 in the string with Cat

Execute method is to execute Regular Expression search on the specified string. The match object and matches set are involved here. The matches set is the set of matching objects. The matches set contains several independent match objects, which can only be created using the execute method of the Regexp object. Example:
The 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' is used to set Case sensitivity.
RegEx. Global = true' to set full availability.
Set matches = RegEx. Execute (strng) 'to execute the search.
For each match in matches 'traverses the matches set.
Retstr = retstr & Match. firstindex &". The matching length is "&""
Retstr = retstr & Match. Length &""
Retstr = retstr & matches (0) & "" 'value: 123
Retstr = retstr & matches (1) & "" 'value: 44
Retstr = retstr & Match. Value & "" 'array with values of 123 and 44
Retstr = retstr & vbcrlf
Next
Regexptest = retstr
End Function
Msgbox (regexptest ("/d +", "123a44 "))

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.