A detailed description of the regular expression capturing group and the non-capturing group of PHP

Source: Internet
Author: User
Tags php regular expression
This paper mainly introduces the regular expression capturing group and the non-capturing group of PHP, and explains the difference between the regular expression capturing group and the non-capturing group of PHP through an example. We hope to help you.

In the project development process is often used, it can be said that regular expression is the most basic requirements of each programmer, beginners in the first contact with the regular expression is very difficult. Recently saw a friend's blog written by the "PHP regular expression" has benefited a lot, in the chapter on wildcards and capture data is very interested. These two chapters also address the content of capturing and non-capturing groups of regular expressions to analyze this.

We know that under the regular expression (x) the match ' X ' is indicated and the matching value is recorded. This is only a more popular statement, and even that it is not rigorous, only () Capture group form will record the matching values. Non-capturing groups are only matched, not logged.

  Capture Group:

(pattern)

This form is one of the most common forms we see, matching and returning capture results, which can be nested, with the order of group numbers arranged from left to right.

$regex = '/(AB (c) +) +d (e)?/';    $str = ' ABCCDE '; $matches = Array (); if (Preg_match ($regex, $str, $matches)) {    print_r ($matches);}


Matching results:

Array ([0] = ABCCDE [1] = ABCC [2] = = c [3] = = e)

(? P<name>pattern)
This approach appears to be slightly more complex when constructing regular expressions, but essentially the same as (pattern). The biggest advantage is in the result processing, the programmer can directly according to their own set of <name> direct quick call results, without the need to go to the number of results in the first few subgroups.

$regex = '/(? P<group1>\w (? P<GROUP2>\W)) ABC (? p<group3>\w) 45/'; $str = ' fsabcd45 '; $matches = Array (); if (Preg_match ($regex, $str, $matches)) {    print_r ($matches);}


Matching results:

Array ([0] = = FSABCD45 [group1] = FS [1] = = FS [group2] + s [2] = [GROUP3] + d [3] = + D)

\num

Num is an integer that is a reverse reference to the capturing group. For example, \2 represents the second subgroup match value, \ represents the first subgroup match value

$regex = '/(\w) (\w) \2\1/';    $str = ' abba '; $matches = Array (); if (Preg_match ($regex, $str, $matches)) {    print_r ($matches);}

Matching results:

Array ([0] = ABBA [1] = a [2] = b)


Note that I overlooked a small detail here, and at first my initial code was $regex = "/(\w) (\w) \2\1/"; Results returned no matching results, after debugging, found here can only use ". ' and ' usage differences people still need to be careful.

 \k< name >


Understand the (? P<name>pattern) and \num, this is not difficult to understand. \k< name > is a reverse reference to a named capture group. Where name is the capturing group name.

$regex = '/(? p<name>\w) abc\k<name>/'; $str = "FABCF"; Echo Preg_match_all ($regex, $str, $matches);p Rint_r ($matches);

Matching results:

Array ([0] = = Array ([0] = FABCF) [name] = = Array ([0] = f) [1] = = Array ([0] = f))

Non-capturing group:

(?:p Attern)

The only difference from (pattern) is that it matches pattern but does not capture matching results. This is no longer an example.

There are four other ways to actually talk about a thing: pre-check.

Pre-check is divided into forward pre-check and reverse pre-check. According to the literal understanding, forward pre-check is to determine whether certain characters after the matching string exist or not, and reverse pre-check is to determine whether certain characters in front of the matching string exist or not.

Positive pre-check the existence of the use (? =pattern), the judgment does not exist in use (?! pattern).

The reverse pre-check is used (? <=pattern) to determine that there is no use (? <!pattern).

$REGX = '/(? <=a) BC (? =d)/'; $str = "ABCD Ebcd abce EBCA"; if (Preg_match_all ($REGX, $str, $matches)) {    Print_r ($ matches);}

Matching results:

Array ([0] = = Array ([0] = BC))


Whether these four forms are used as long as the position and assertion of the relative matching string are positive or negative, they will be mastered very quickly.

In addition, the pre-check of four forms is 0 width, the matching time only to make a judgment, itself is not accounted for position. /he (? =l) llo/matches Hello, while/he (? =l) lo/does not match hello. After all, there are only 4 of them, but there are 5 of them in the number of bytes.

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.