VBS tutorial: introduction to regular expressions – regular expression syntax

Time:2022-5-7

regular expression syntax

A regular expression consists of ordinary characters (such as characters a to Z) and special characters (calledMetacharacter)Text mode composed of. This pattern describes one or more strings to match when finding the body of text. The regular expression is used as a template to match a character pattern with the searched string.

Here are some examples of regular expressions you might encounter:

Visual Basic Scripting Edition VBScript matching
/^\[ \t]*$/ “^\[ \t]*$” Match a blank line.
/\d{2}-\d{5}/ “\d{2}-\d{5}” Verify that an ID number consists of a 2-digit number, a hyphen, and a 5-digit number.
/<(.*)>.*<\/\1>/ “<(.*)>.*<\/\1>” Match an HTML tag.

The following table is a complete list of metacharacters and their behavior in the context of regular expressions:

character describe
\ Marks the next character as a special character, or a literal character, or a backward reference, or an octal escape character. For example, ‘n’ matches the character ‘n’. ‘ \N ‘matches a newline character. The sequence ‘\ \’ matches’ \ ‘and’ \ (‘matches’ (‘.
^ Matches the start of the input string. If setRegExpObjectMultilineAttribute, ^ also matches the position after ‘\ n’ or ‘\ R’.
$ Matches the end of the input string. If setRegExpObjectMultilineProperty, $also matches the position before ‘\ n’ or ‘\ R’.
* Matches the previous subexpression zero or more times. For example, Zo * can match “Z” and “zoo”* Equivalent to {0,}.
+ Matches the previous subexpression one or more times. For example, ‘Zo +’ can match “Zo” and “zoo”, but not “Z”+ Equivalent to {1,}.
? Matches the previous subexpression zero or once. For example, “do (ES)” You can match “do” in “do” or “does”.? Equivalent to {0,1}.
{n} nIs a nonnegative integer. Match determinednTimes. For example, ‘O {2}’ cannot match ‘o’ in “Bob”, but it can match two o’s in “food”.
{n,} nIs a nonnegative integer. At least matchnTimes. For example, ‘O {2,}’ cannot match ‘o’ in ‘Bob’, but can match all o’s in ‘fooood’ O {1,} ‘is equivalent to’ O + ‘ O {0,} ‘is equivalent to’ o * ‘.
{n,m} mandnAre non negative integers, wheren <= m。 Least matchnTimes and matches at mostmTimes. Liu, “O {1,3}” will match the first three o’s in “food” O {0,1} ‘is equivalent to’ o? ‘. Please note that there can be no space between comma and two numbers.
? When this character is immediately followed by any other qualifier (*, +{n}, {n,}, {n,m}) later, the matching pattern is non greedy. The non greedy pattern matches as few strings as possible, while the default greedy pattern matches as many strings as possible. For example, for the string “oooo”,’O +? ‘ A single ‘o’ will be matched and ‘O +’ will match all ‘o’.
. Matches any single character except ‘\ n’. To match any character including ‘\ n’, use a pattern like ‘[. \ n]’.
(pattern) matchingpatternAnd get this match. The obtained matches can be obtained from the generated matches collection and used in VBScriptSubMatchesCollection, which is used in Visual Basic Scripting Edition$0$9Properties. To match parenthesis characters, use ‘\ (‘ or ‘\)’.
(?:pattern) matchingpatternHowever, the matching result is not obtained, that is, it is a non obtained match and will not be stored for future use. This is useful when using the or character (|) to combine parts of a pattern. For example, ‘industry (: y|ies) is a simpler expression than’ industry|industries’.
(?=pattern) Forward pre check, in any matchpatternMatches the lookup string at the beginning of the string. This is a non fetched match, that is, the match does not need to be fetched for later use. For example, ‘windows (? = 95|98|nt|2000)’ can match “windows” in “Windows 2000”, but cannot match “windows” in “windows 3.1”. The pre check does not consume characters, that is, after a match occurs, the search for the next match starts immediately after the last match, rather than after the characters containing the pre check.
(?!pattern) Negative lookahead matches the search string at any point where a string not matchingpatternMatches the lookup string at the beginning of the string. This is a non fetched match, that is, the match does not need to be fetched for later use. For example, ‘windows (?! 95|98|nt|2000)’ can match “windows” in “windows 3.1”, but cannot match “windows” in “Windows 2000”. The pre check does not consume characters, that is, after a match occurs, the search for the next match starts immediately after the last match, rather than after the characters containing the pre check
x|y matchingxory。 For example, ‘z|food’ can match “Z” or “food” (z|f) ood ‘matches “zoo” or “food”.
[xyz] Character set. Match any character contained. For example, ‘[ABC]’ can match ‘a’ in ‘plain’.
[^xyz] Negative character set. Matches any characters that are not included. For example, ‘[^ ABC]’ can match ‘p’ in ‘plain’.
[a-z] Character range. Matches any character within the specified range. For example, ‘[A-Z]’ can match any lowercase character in the range of ‘a’ to ‘Z’.
[^a-z] Negative character range. Matches any character that is not within the specified range. For example, ‘[^ A-Z]’ can match any character that is not in the range of ‘a’ to ‘Z’.
\b Match a word boundary, that is, the position between the word and the space. For example, ‘er \ B’ can match ‘er’ in ‘never’, but not ‘er’ in ‘verb’.
\B Matches non word boundaries. ‘ Er \ B ‘can match’ er ‘in’ verb ‘, but cannot match’ er ‘in’ never ‘.
\cx Match byxThe specified control character. For example, \ cm matches a control-m or carriage return.xThe value of must be either A-Z or one of A-Z. Otherwise, C is treated as a literal ‘C’ character.
\d Match a numeric character. Equivalent to [0-9].
\D Matches a non numeric character. Equivalent to [^ 0-9].
\f Match a page feed. Equivalent to \ x0c and \ CL.
\n Match a newline character. Equivalent to \ x0a and \ CJ.
\r Match a carriage return. Equivalent to \ x0d and \ cm.
\s Matches any white space 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 a tab. Equivalent to \ X09 and \ CI.
\v Match a vertical tab. Equivalent to \ x0B and \ CK.
\w Matches any word characters that include underscores. Equivalent to ‘[a-za-z0-9#]’.
\W Matches any non word characters. Equivalent to ‘[^ a-za-z0-9]’.
\xn matchingn, wherenIs a hexadecimal escape value. Hexadecimal escape value must be two digits long. For example, ‘\ x41′ matches’ a ‘.’ \X041 ‘is equivalent to’ \ X04 ‘& “1”. ASCII encoding can be used in regular expressions
\num matchingnum, wherenumIs a positive integer. A reference to the match obtained. For example, ‘(.)\ 1 ‘matches two consecutive identical characters.
\n Identifies an octal escape value or a backward reference. If\nBefore at leastnGets a subexpression, thennIs a backward reference. Otherwise, ifnOctal digits (0-7), thennIs an octal escape value.
\nm Identifies an octal escape value or a backward reference. If\nmAt least is preceded by at leastnmGets a subexpression, thennmIs a backward reference. If\nmAt least beforenGet, thennIs a text followed bym Backward reference of. If the previous conditions are not met, ifnandmAre octal digits (0-7), then\nmWill match octal escape valuesnm
\nml IfnIs an octal digit (0-3), andmandlIf they are all octal digits (0-7), the octal escape value will be matchednml。
\un matchingn, wherenIs a Unicode character represented by four hexadecimal digits. For example, \ u00a9 matches the copyright symbol (?).