Microsoft Regular Expression tutorial (3): character matching

Source: Internet
Author: User
Tags printable characters
Common characters

A common character consists of all the print and non-print characters that are not explicitly specified as metacharacters. This includes all uppercase and lowercase letter characters, all numbers, all punctuation marks, and some symbols.

The simplest regular expression is a single normal character that can match the character itself in the searched string. For example, the single-Character Mode 'A' can match the 'A' letter that appears at any position in the searched string '. Here are some examples of Single-character regular expression patterns:

/a/ /7/ /M/

The equivalent single-character Regular Expression of VBScript is:

"a" "7" "M"

You can combine multiple single characters to obtain a large expression. For example, the following JScript regular expression is an expression created by combining a single character expression 'A', '7', and 'M.

/a7M/

The equivalent VBScript expression is:

"a7M"

Note that there is no join operator here. All you need to do is place one character after the other.

Special characters

Many metacharacters require special processing when trying to match them. To match these special characters, you must first escape these characters, that is, use a backslash (/) before them (/). The following table lists the special characters and their meanings:

Special characters Description
$ Matches the end position of the input string. IfRegexpObjectMultiline$ Also matches '/N' or'/R '. To match the $ character, use/$.
() Mark the start and end positions of a subexpression. Subexpressions can be obtained for future use. To match these characters, use/(and /).
* Matches the previous subexpression zero or multiple times. To match * characters, use /*.
+ Match the previous subexpression once or multiple times. To match + characters, use/+.
. Match any single character except linefeed/n. To match., use /.
[ Mark the start of a bracket expression. To match [, use /[.
? Match the previous subexpression zero or once, or specify a non-Greedy qualifier. To match? Character, use /?.
/ Mark the next character as a special character, or a literal character, or backward reference, or an octal escape character. For example, 'n' matches the character 'n '. '/N' matches the line break. The sequence '//' matches "/", while '/(' matches "(".
^ Matches the start position of the input string. Unless used in the square brackets expression, this character set is not accepted. To match the ^ character itself, use/^.
{ Mark the start of a qualifier expression. To match {, use /{.
| Specifies a choice between two items. To match |, use/|.


Non-printable characters

There are many useful non-printable characters that must be used occasionally. The following table lists escape sequences used to indicate non-printable characters:

Character Description
/CX MatchingXThe specified control character. For example,/cm matches a control-M or carriage return character.XMust be a A-Z or one of a-Z. Otherwise, C is treated as an original 'C' character.
/F Match a form feed. It is equivalent to/x0c and/Cl.
/N Match A linefeed. It is equivalent to/x0a and/CJ.
/R Match a carriage return. It is equivalent to/x0d and/cm.
/S Matches any blank characters, including spaces, tabs, and page breaks. It is equivalent to [/f/n/R/T/V].
/S Match any non-blank characters. It is equivalent to [^/f/n/R/T/V].
/T Match a tab. It is equivalent to/x09 and/CI.
/V Match a vertical tab. It is equivalent to/x0b and/ck.


Character matching

The period (.) matches any single print or non-print character in a string, except the line break (/N. The following JScript regular expressions can match 'aac ', 'abc', 'Acc', and 'adc, you 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 containing a file name, where the period (.) is a part of the input string, you can add a backslash (/) character before the period in the regular expression to achieve this requirement. For example, the following JScript regular expression can match 'filename. Ext ':

/filename/.ext/

For VBScript, the equivalent expression is as follows:

"filename/.ext"

These expressions are still quite limited. They only allow matchingAnySingle character. In many cases, it is useful to match special characters from the list. For example, if the input text contains chapter titles such as Chapter 1 and Chapter 2, you may need to find these chapter titles.

Parentheses

You can add one or more single characters to a square brackets ([and]) to create a list to be matched. If the character is enclosed in parentheses, the list is calledParentheses. Like any other place in brackets, normal characters represent themselves, that is, they match one of their own in the input text. Most special characters will lose their meaning in the brackets expression. Here are some exceptions:

  • ']' If it is not the first character, a list is ended. To match the ']' character in the list, place it in the first item, followed by the START.
  • '/' Is still used as an escape character. To match the '/' character, use '//'.

The characters contained in the brackets expression only match a single character that is located in the regular expression. The following JScript regular expressions can match 'Chapter 1', 'Chapter 2', 'Chapter 3', 'Chapter 4', and 'Chapter 5 ':

/Chapter [12345]/

To match the same chapter title in VBScript, use the following expression:

"Chapter [12345]"

Note that the word 'Chapter 'and the Space following it have a fixed relationship with the character positions in the brackets. Therefore, the bracket expression is only used to specify a character set that matches the position of a single character following the word 'Chapter 'and a space. This is the ninth character.

If you want to use a range rather than a character to represent the characters to be matched, you can use a hyphen to separate the start and end characters of the range. The character values of each character determine their relative sequence within a range. The following JScript regular expression contains a range expression equivalent to the list of parentheses shown above.

/Chapter [1-5]/

The expressions for the same functions in vbscipt are as follows:

"Chapter [1-5]"

If the range is specified in this way, the start and end values are included in the range. Note that the starting value in Unicode sorting must be before the ending value.

To include a hyphen in a bracket expression, you must use one of the following methods:

  • Escape it with a backslash:
    [/-]
  • Place the hyphens at the beginning and end of the brackets list. The following expression can match all lowercase letters and hyphens:
    [-a-z] [a-z-]
  • Create a range where the start character value is less than the connected character, and the end character value is equal to or greater than the connected character. The following two regular expressions meet this requirement:
    [!--] [!-~]

Similarly, by placing an insert character (^) at the beginning of the list, you can find all characters that are not in the list or range. If the plug-in is located elsewhere in the list, it matches itself without any special meaning. The following JScript Regular Expression matches the title of a chapter whose chapter number is greater than 5:

/Chapter [^12345]/

For VBScript:

"Chapter [^12345]"

In the preceding example, the expression matches any number characters except 1, 2, 3, 4, or 5 at the ninth position. Therefore, 'Chapter 7' is a match, as is 'Chapter 9.

The above expression can be expressed by hyphens. For JScript:

/Chapter [^1-5]/

Or, for VBScript:

"Chapter [^1-5]"

A typical use of a bracket expression is to specify a match for any uppercase or lowercase letter or number. The following JScript expression provides 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.