Introduction of regular Expressions (Microsoft)--10. Character Matching

Source: Internet
Author: User
Tags character set contains expression lowercase range regular expression
Microsoft | regular 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:

1. '] ' character if it is not the first item, a list will be closed. To match the '] ' character in the list, place it in the first item, immediately after the start ' ['.

2. ' \ ' 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:

1. Use the backslash to escape it: [\-]

2. Place hyphens at the beginning and end of the parentheses list. The following expression matches all lowercase letters and hyphens: [-a-z],[a-z-]

3. Create 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]"



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.