VBS tutorial: VBScript attribute – pattern attribute

Time:2022-1-2

Pattern attribute

Sets or returns the regular expression pattern to be searched.

object.Pattern [= "searchstring"]

parameter

object

Required. Always oneRegExpObject variable.

searchstring

Optional. The regular string expression to be searched. It may containset upVarious regular expression characters in some tables.

set up

Special characters and sequences are used when writing patterns for regular expressions. The following table describes the characters and sequences that can be used and gives examples.

character describe
\ Marks the next character as a special character or literal. For example, “n” matches the character “n”. “\ n” matches a newline character. The sequence “\ \” matches “\” and “\ (” matches “(“.
^ Match the start position of the input.
$ Matches the end of the input.
* Matches the previous character zero or several times. For example, “Zo *” can match “Z”, “zoo”.
+ Matches the previous character one or more times. For example, “Zo +” can match “zoo”, but not “Z”.
? Matches the previous character zero or once. For example, “a? Ve?” You can match “ve” in “never”.
. Matches any character other than a newline character.
(pattern) Match the pattern and remember the match. Matching substrings can be obtained from theMatchesUse item in collection[0]…[n]obtain. If you want to match parenthesis characters (and), use “\ (” or “\)”.
x|y matchingxory。 For example, “z|food” can match “Z” or “food”. “(z|f) OO” matches “zoo” or “food”.
{n} nIs a nonnegative integer. Just matchnTimes. For example, “O {2}” cannot match “O” in “Bob”, but can match the first two o’s in “fooood”.
{n,} nIs a nonnegative integer. Match at leastnTimes. For example, “O {2,}” does not match “O” in “Bob”, but matches all o’s in “fooood”. “O {1,}” is equivalent to “O +”. ‘O {0,}’ is equivalent to ‘o *.’.
{n,m} mandnIs a nonnegative integer. Match at leastnTimes, at mostmTimes. For example, “O {1,3}” matches the first three o’s in “food”. “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] Represents a range of characters. Matches any character in the specified interval. For example, “[A-Z]” matches any lowercase character between “a” and “Z”.
[^m-z] Negative character interval. Matches characters that are not within the specified range. For example, “[M-Z]” matches any character that is not between “m” and “Z”.
\b Matches the boundary of the word, that is, the position between the word and the space. For example, “Er \ B” matches “Er” in “never”, but does not match “Er” in “verb”.
\B Matches non word boundaries. “EA * r \ B” matches “ear” in “never early”.
\d Matches a numeric character. Equivalent to [0-9].
\D Non matching characters with numbers. Equivalent to [^ 0-9].
\f Matches the page break.
\n Matches the newline character.
\r Matches the carriage return character.
\s Matches any white characters, including spaces, tabs, page breaks, and so on. Equivalent to “[\ f \ n \ R \ t \ v]”.
\S Matches any non whitespace characters. Equivalent to “[^ \ f \ n \ R \ t \ v]”.
\t Match tab.
\v Matches the vertical tab.
\w Matches any word character, including underscores. Equivalent to “[a-za-z0-9#]”.
\W Matches any non word character. Equivalent to “[^ a-za-z0-9]”.
\num matchingnumOf whichnumIs a positive integer. Reference back to the remembered match. For example, “(.)\ 1 “matches two consecutive identical characters.
\n matchingn, wherenIs an octal escape value. Octal escape value must be 1, 2 or 3 digits long. For example, both “\ 11” and “\ 011” match a tab. “\ 0011” is equivalent to “\ 001” and “1”. Octal escape value shall not exceed 256. Otherwise, only the first two characters are considered part of the expression. ASCII code is allowed in regular expressions.
\xn matchingn, wherenIs a hexadecimal escape value. Hex escape value must be exactly two digits long. For example, “\ x41” matches “a”. “\ x041” is equivalent to “\ X04” and “1”. ASCII code is allowed in regular expressions.

explain

The following code illustratesPatternProperty usage:

Function regexptest (PATRN, strng) dim regex, match, matches' create variables. Set regex = new regexp 'establishes a general expression. regEx. Pattern = pattern 'set the pattern. regEx. Ignorecase = true 'sets whether it is case sensitive. regEx. Global = true 'set global availability. set Matches=regEx. Execute (string) 'duplicate matching set regexptest = regex Execute (strng) 'execute the search. For each match in matches' duplicate matching set retstr = retstr & "match found at position" retstr = retstr & match FirstIndex&".Match Value is '"RetStr=RetStr&Match. Value&"'."& vbCRLF NextRegExpTest=RetStrEnd FunctionMsgBox(RegExpTest("is.",  "IS1 is2 IS3 is4"))