Difference between regular expressions (parentheses), [brackets], and {braces }.

Source: Internet
Author: User

Difference between regular expressions (parentheses), [brackets], and {braces }.

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

() Is used to extract matching strings. When there are several () in the expression, there are several matching strings.

(\ S *) indicates a string with consecutive spaces.

[] Is to define the matching character range. For example, [a-zA-Z0-9] indicates that characters at the corresponding position must match English characters and numbers. [\ S *] indicates a space or * number.

{} Is generally used to indicate the length of a match. For example, \ s {3} indicates that three spaces are matched, and \ s [1, 3] indicates that one or three spaces are matched.

(0-9) matches '0-9' itself. [0-9] * match a number (Note that there is * behind it, which can be blank) [0-9] + match a number (Note that there is + behind it, and cannot be blank) {1-9} incorrect syntax.

[0-9] {0, 9} indicates a string of numbers ranging from 0 to 9.

For example, in PHP, the filter content contains numbers or spaces.

Copy codeThe Code is as follows:
Preg_replace ("/\ d {1,} \ s {0, 1}/", "xxxxxxxx", $ signaturecontent );


What is the difference between parentheses in a regular expression and brackets?

The most basic meaning is that parentheses are regarded as a whole, and the brackets match one of them, and the braces match several times.

However, adding other characters to the brackets may have different meanings. For example:

{N}
N is a non-negative integer. Match n times. For example, "o {2}" cannot match "o" in "Bob", but can match two o in "food.

{N ,}
N is a non-negative integer. Match at least n times. For example, "o {2,}" cannot match "o" in "Bob", but can match all o in "foooood. "O {1,}" is equivalent to "o + ". "O {0,}" is equivalent to "o *".

{N, m}
Both m and n are non-negative integers, where n <= m. Match at least n times and at most m times. For example, "o {1, 3}" matches the first three o in "fooooood. "O {0, 1}" is equivalent to "o ?". Note that there must be no space between a comma and two numbers.

?
When this character is followed by any other delimiter (*, + ,?, The matching mode after {n}, {n ,}, {n, m}) is not greedy. Non-Greedy Mode
The default greedy mode matches as many searched strings as possible. For example, for strings "oooo", "o + ?" Will match a single "o", while "o +"
All "o" will be matched ".

.
Match any single character except "\ n. To match any character including "\ n", use a pattern like "[. \ n.

(Pattern)
Match pattern and obtain this match. The obtained match can be obtained from the generated Matches set. The SubMatches set is used in VBScript, and $0… is used in JScript... $9 attribute. To match the parentheses, use "\ (" or "\)".

(? : Pattern)
Matches pattern but does not get the matching result. That is to say, this is a non-get match and is not stored for future use. This is useful when you use the "(|)" character to combine all parts of a pattern. For example, "industr (? : Y | ies) "is a simpler expression than" industry | industrial.

(? = Pattern)
Forward pre-query: matches the search string at the beginning of any string that matches the pattern. This is a non-get match, that is, the match does not need to be obtained for later
. For example (? = 95 | 98 | NT | 2000) "can match" Windows "in" Windows2000 ", but cannot match
"Windows" in "Windows3.1 ". Pre-query does not consume characters, that is, after a match occurs, the next matching search starts immediately after the last match, instead of starting from
Starts after the pre-query characters are included.

(?! Pattern)
Negative pre-query: matches the search string at the beginning of any string that does not match pattern. This is a non-get match, that is, the match does not need to be obtained
. For example, "Windows (?! 95 | 98 | NT | 2000) "can match" Windows "in" Windows3.1 ", but cannot match
Windows in "Windows2000 ". Pre-query does not consume characters, that is, after a match occurs, the next matching search starts immediately after the last match, instead
Starts after the pre-query characters.

X | y
Match x or y. For example, "z | food" can match "z" or "food ". "(Z | f) ood" matches "zood" or "food ".

[Xyz]
Character Set combination. Match any character in it. For example, "[abc]" can match "a" in "plain ".

[^ Xyz]
Negative value character set combination. Match any character not included. For example, "[^ abc]" can match "p" in "plain ".

[A-z] ...... the remaining full text>

What are the differences between the brackets () [] {} in a regular expression?

() This bracket will allocate a bucket and you can use $1 to get the data in it.
[] Use a custom expression that can match multiple characters. For example, [mike] matches the four letters, m, I, k, and e. Note that a single character cannot match the word mike, if you want to match a word, you can write it like this (mike). If you do not need to allocate a bucket, you can write mike directly.
{} Indicates the number of matches. A {0, 1} a appears at least 0 times or at most 1 time. The second appearance does not match a {} a at least 2 times or at most 5 times.
Hope you can understand it!

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.