Character matching
a period (.) matches any single printed or nonprinting character in a string, except for line breaks (\ n). The following Visual Basic scripting Edition Regular expressions can match ' AAC ', ' abc ', ' ACC ', ' ADC ' and so on, and can also match ' A1c ', ' a2c ', a-c ' and a#c ':
/a.c/
The equivalent VBScript regular expression is:
"a.c"
If you try to match a string that contains a filename, where the period (.) is part of the input string, you can implement this requirement by preceding the period in the regular expression with a backslash (\) character. For example, the following Visual Basic scripting Edition Regular expressions can match ' filename.ext ':
/filename\.ext/
For VBScript, an equivalent expression looks like this:
"filename\.ext"
These expressions are still quite limited. They only allow matching of any single character. In many cases, it is useful to match Special characters from a list. For example, if your input text contains chapter headings that are represented numerically as Chapter 1, Chapter 2, and so on, you may need to find these chapter headings.
Bracket expression
You can create a list to match by putting one or more single characters in a square bracket ([and]). If the character is enclosed in parentheses, the list is called a bracket expression . In parentheses and anywhere else, ordinary characters represent themselves, that is, they match the one that appears in the input text. Most special characters lose their meaning when they are in a bracket expression. Here are some exceptions:
- The '] ' character will end a list if it is not the first item. To match the '] ' character in the list, place it in the first item, immediately after the start ' ['.
- ' \ ' is still an escape character. to match the ' \ ' character, please use ' \ '.
The characters contained in the bracket expression match only a single character of the bracket expression where it is located in the regular expression. The following Visual Basic scripting Edition Regular expressions can match ' Chapter 1 ', ' Chapter 2 ', ' Chapter 3 ', ' Chapter 4 ', and ' Chapter 5 ':
/Chapter [12345]/
To match the same chapter headings in VBScript, use the following expression:
"Chapter [12345]"
Note that the word ' Chapter ' and the following spaces are fixed with the position of the characters in parentheses. Therefore, the bracket expression is used only to specify a character set that satisfies the single characters position immediately following the word ' Chapter ' and a space. This is the Nineth character position.
If you want to use a range instead of the character itself to represent a character to be matched, you can use a hyphen to separate the start and end characters of the range. The character value of each character determines its relative order within a range. The following Visual Basic scripting Edition Regular expression contains a range expression that is equivalent to the list of parentheses shown above.
/Chapter [1-5]/
An expression of the same functionality in Vbscipt is shown below:
"Chapter [1-5]"
If you specify a range in this manner, both the start and end values are included within that range. It is particularly important to note that the starting value in a Unicode sort must precede the end value.
If you want to include hyphens in parentheses expressions, you must use one of the following methods:
- Use a backslash to escape it:
[\-]
- Place hyphens at the beginning and end of the parentheses list. The following expression matches all lowercase letters and hyphens:
[-a-z][a-z-]
- Creates a range where the value of the start character is less than the hyphen, and the end character's value is equal to or greater than the hyphen. The following two regular expressions all meet this requirement:
[!--][!-~]
Similarly, by placing an caret (^) at the beginning of the list, you can find all characters that are not in the list or range. If the caret appears elsewhere in the list, it matches itself without any special meaning. The following Visual Basic scripting Edition a regular expression that matches a section heading with a chapter number greater than 5:
/Chapter [^12345]/
For VBScript use:
"Chapter [^12345]"
In the example shown above, the expression matches any numeric character except 1, 2, 3, 4, or 5 in the nineth position. So, ' Chapter 7 ' is a match, same ' Chapter 9 '.
The expression above can be expressed using a hyphen (-). For Visual Basic scripting Edition is:
/Chapter [^1-5]/
Or, for VBScript:
"Chapter [^1-5]"
A typical use of bracket expressions is to specify a match for any uppercase or lowercase alphabetic characters or any number. The following Visual Basic scripting Edition expression gives this match:
/[A-Za-z0-9]/
The equivalent VBScript expression is:
"[A-Za-z0-9]"