ASP VBScript Regular Expression Regexp object

Source: Internet
Author: User

Regexp objects support simple regular expressions.

  Regexp object usage:
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 "))

  TheAttribute

◎ 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 ):
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 ):
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), 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, "ER/B" matches "er" in "never", but does not match "er" in "verb ".
/B matches the non-word boundary. "EA * r/B" matches "ear" in "never early.
/D matches a numeric character. 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 "[/f/n/R/T/V]".
/S matches any non-blank characters. It is equivalent to "[^/f/n/R/T/V]".
/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, "/11" and "/011" Both match a tab. "/0011" is equivalent to "/001" 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:
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 "))

  TheMethod

  ◎ 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:
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:
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 +)", "$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.

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 "))

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.