Several common methods of ASP Regular Expressions (execute, test, replace)

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, you need to replace msgboxResponse. WriteCopyCodeThe Code is 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 codeCopy codeThe Code is 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, which is to execute a regular expression search for 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:
vbs test code copy Code the code is as follows: function regexptest (patrn, strng)
dim RegEx, match, matches 'to create a variable.
set RegEx = new Regexp 'creates a regular expression.
RegEx. pattern = patrn 'sets the mode.
RegEx. ignorecase = true' is used to set Case sensitivity.
RegEx. Global = true' sets full availability.
set matches = RegEx. Execute (strng.
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. array with value & "" 'values 123 and 44
retstr = retstr & vbcrlf
next
regexptest = retstr
end function
msgbox (regexptest ("\ D + ", "123a44")

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.