Regular Expressions (parentheses), [brackets], {curly braces} differences Summary of the regular expression

Source: Internet
Author: User

The () [] {} of the regular expression has different meanings.

() is to extract a matching string. Several () of the expressions have several corresponding matching strings.

(\s*) A string that represents a contiguous space.

[] is the range of characters that define the match. For example, [a-za-z0-9] represents the corresponding position of characters to match English characters and numbers. [\s*] denotes a space or * number.

{} is typically used to indicate a matching length, such as \s{3} to match three spaces, \s{1,3} to match one to three spaces.

(0-9) match ' 0-9′ itself. [0-9]* Matching digits (note that there are *, can be empty) [0-9]+ matching digits (note that there are +, not nullable) {1-9} spelling errors.

[0-9] {0,9} represents a numeric string that is 0 to 9 in length.

————————————————————————————————————————————————————————————

parentheses () are groups that are mainly used to limit the range/group/Capture text/glance/special mode processing of multiple-selection structures
Example:
1, (ABC|BCD|CDE), that this paragraph is ABC, BCD, CDE one of the three can be, the order must also be consistent
2, (ABC)?, means that this group either appears together or does not appear, and appears in the order in which they appear
3. (?: ABC) indicates that a group such as ABC is found, but not recorded, and not saved to the $ variable, otherwise it can be achieved by $x the items that are matched by the parentheses, such as: (AAA) (BBB) (CCC) (?:D dd) (EEE), which can be used to match the contents of (AAA) to, and $ 3 Get the (CCC) match to the content, and $ $ is obtained by (EEE) match to the content, because the first pair of parentheses did not save the variable
4, a (? =bbb) order to look around to indicate that a must be followed by 3 consecutive B
5. (? i:xxxx) does not match case (? s:.*) across rows. You can match a return character

square brackets are a single match , character set/exclude character set/named character set
Example:
1, [0-3], to find that the characters in this position can only be 0 to 3 of the four digits, compared to (ABC|BCD|CDE), but the parentheses can match multiple consecutive characters, and one pair of brackets can only match a single character
2. [^0-3], which indicates that the character found in this position can only be all characters except 0 to 3.
3, [:d igit:] 0-9 [: alnum:] A-za-z0-9

——————————————————————————————————————————————————————————————————————

() and [] have the essential difference
The content in the () represents a subexpression, () itself does not match anything, nor does it restrict the matching of anything, but only the contents of parentheses are treated as one expression, for example (AB) {1,3}, which means that AB appears at least 1 times in a row, up to 3 times. If there is no parenthesis, ab{1,3}, which means a, followed by the following b appears at least 1 times, up to 3 times. In addition, parentheses are also important in matching patterns. This is not extended, LZ are interested in their own to check
[] indicates that the matching characters are in [] and can only occur once, and that special characters written in [] will be matched as normal characters. For example [(a)], matches (, A,), these three characters.
So () [] the meaning of action or expression, there is a difference, there is no connection
————————————————————————————————————————————————————————————————

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.