PHP's regular expression capturing group and non-capture group (detailed) _php instance

Source: Internet
Author: User
Tags php regular expression regular expression

In the project development process is often said to use, can be said that regular expression is the most basic requirements of each programmer, beginners in just contact with regular expressions are very difficult. A friend's blog recently saw a lot of benefits from the PHP regular expression, which is very interesting in the chapters for wildcard characters and capturing data. These two chapters also cover the contents of capture groups and non-capture groups of regular expressions to analyze this aspect

We know that, under regular expressions (x), Matches ' X ' and records a matching value. This is only a more popular version, and even said that this is not strict, only () Capture group form will record matching values. Non-capture groups are matched only, not logged.

  Capture Group:

(pattern)

This form is one of the most common forms we see, matching and returning the captured results, nesting, and ordering the group numbers from left to right.

Copy Code code as follows:

$regex = '/(AB (c) +) +d (e)?/';
$str = ' ABCCDE ';
$matches = Array ();

if (Preg_match ($regex, $str, $matches)) {
Print_r ($matches);
}

Match result:

Copy Code code as follows:

Array ([0] => ABCCDE [1] => ABCC [2] => C [3] => e)
(? P<name>pattern)

This approach, although it seems slightly more complex when constructing regular expressions, is essentially the same as (pattern). The biggest advantage is that in the processing of results, programmers can call the results directly based on their own <name> directly, without having to count the desired results in the first few subgroups.


Copy Code code as follows:

$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);
}

Match result:

Copy Code code as follows:

Array ([0] => fsabcd45 [group1] => FS [1] => FS [group2] => s [2] => s [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


Copy Code code as follows:

$regex = '/(\w) (\w) \2\1/';
$str = ' ABBA ';
$matches = Array ();

if (Preg_match ($regex, $str, $matches)) {
Print_r ($matches);
}

Match result:

Copy Code code as follows:

Array ([0] => ABBA [1] => a [2] => B)

Note that here I overlooked a little detail, and at first I was like code $regex = "/(\w) (\w) \2\1/"; The result returns no match result, after debugging, found here can only use '. "and" The use of different people still need to pay attention to.

 \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.


Copy Code code as follows:

$regex = '/(? p<name>\w) abc\k<name>/';

$str = "FABCF";

Echo Preg_match_all ($regex, $str, $matches);

Print_r ($matches);

Match result:

Copy Code code as follows:

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

Non-capture group:

(?:p Attern)

The only difference from (pattern) is that match pattern does not capture the matching result. Here is no longer an example.

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

Pre-check is divided into forward and reverse check. According to literal understanding, forward lookup is to determine the existence of certain characters following the matching string, while the reverse lookup is to determine the existence of certain characters before the matching string.

Forward check to determine the existence of the use of (? =pattern), to determine the existence of the use of (?! pattern).

The reverse lookup judgment exists using (? <=pattern) to determine that there is no use (? <!pattern).

Copy Code code as follows:

$REGX = '/(<=a) BC (? =d)/';

$str = "ABCD Ebcd abce EBCA";

if (Preg_match_all ($REGX, $str, $matches)) {

Print_r ($matches);
}

Match result:

Copy Code code as follows:

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

These four forms are used if the position and assertion of the relative matching string is positive or negative, it will be mastered quickly.

In addition, the four forms of the pre-check is 0 width, when matching only make a judgment, itself is not occupy position. /he (? =l) llo/matches Hello, and/he (? =l) lo/does not match hello. After all, but the two are mismatched from the byte number, the former is only 4, and the latter has 5.

The above is the regular expression of PHP capture group and the total content of the non-capture group, I hope to enlighten you.

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.