Character matching and brackets

Source: Internet
Author: User

Character matching and brackets

The period (.) matches various printed or non-printed characters in the string, except for one character. This exception is the line break (\ n ). The following regular expressions match aac, abc, acc, adc, and so on, as well as a1c, a2c, a-c, and a # c:

/a.c/

To match a string containing a file name, and a period (.) is a component of the input string, add the backslash (\) character before the period in the regular expression. For example, the following regular expression matches filename. ext:

/filename\.ext/

These expressions only allow you to match"Any"Single character. You may need to match a specific character group in the list. For example, you may need to search for Chapter titles in numbers (Chapter 1, Chapter 2, and so on ).

Brackets

To create a list of matched character groups, place one or more individual characters in square brackets ([and. This list is called"Brackets". As in any other position, a common character represents itself in brackets, that is, it matches itself once in the input text. Most special characters are meaningless when they appear in the brackets expression. However, there are some exceptions, such:

If the character is not the first, it ends a list. To match the] character in the list, place it first, followed by the start.

The \ character continues as an escape character. To match \ characters, use \\.

The characters in the brackets expression only match a single character at this position in the regular expression. The following regular expressions match Chapter 1, Chapter 2, Chapter 3, Chapter 4, and Chapter 5:

/Chapter [12345]/

Note thatChapterThe positions of spaces are fixed with the characters in brackets. The brackets specify only matching followed by words.ChapterAnd the character set at the position of a single character after the space. This is the ninth character.

To use a range instead of a character to represent a matched character group, use a hyphen (-) to separate the start and end characters in the range. The character values of a single character determine the relative sequence in the range. The following regular expression contains a range expression, which is equivalent to the list in the brackets shown above.

/Chapter [1-5]/

When the range is specified in this way, both the start value and end value are included in the range. Note that it is also important that the start value must be prior to the end value in Unicode order.

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

Escape it with a reverse slice:

[\-]

Place the hyphen at the beginning or end of the brackets list. The following expression matches all lowercase letters and hyphens:

[-A-z] [a-z-] creates a range in which the start character value is less than the hyphen, and the end character value is equal to or greater than the hyphen. The following two regular expressions meet this requirement:
[!--][!-~]

To search for all characters not in the list or range, place the inserted symbol (^) at the beginning of the list. If the inserted character appears anywhere in the list, it matches itself. The following regular expression matches the title of a section with a number greater than 5:

/Chapter [^12345]/

In the preceding example, the expression matches any number character other than 1, 2, 3, 4, or 5 at the ninth position. In this way, for example, Chapter 7 is a matching item, and Chapter 9 is also a matching item.

The above expression can be represented by hyphens:

/Chapter [^1-5]/

A bracket expression is typically used to specify matching of any uppercase or lowercase letter or number. The following expression specifies the matching:

/[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.