PHP regular expression capturing group and non-capturing group (details)

Source: Internet
Author: User
Tags php regular expression
This article mainly introduces the regular expression capturing group and non-capturing group in php. if you need a regular expression, you can refer to the regular expression frequently used during project development, it can be said that regular expressions are the most basic requirement of every programmer. it is difficult for beginners to get started with regular expressions. Recently, I saw a friend's blog writing "PHP regular expression", which has benefited a lot. in this section, I am very interested in wildcards and data capturing. These two chapters also involve the capture group and non-capture group content of the regular expression, so as to analyze this content.

We know that in a regular expression (x), it indicates matching 'X' and record the matching value. This is just a popular saying, or even an not rigorous saying. only the () capture group form records the matching value. Non-capturing groups only match, not recorded.

  Capture Group:

(Pattern)

This is one of the most common forms. matching and returning captured results can be nested and the group numbers are arranged from left to right '.

The code is as follows:


$ Regex = '/(AB (c) + d (e )? /';
$ Str = 'ABCDE ';
$ Matches = array ();

If (preg_match ($ regex, $ str, $ matches )){
Print_r ($ matches );
}


Matching result:

The code is as follows:


Array ([0] => abccde [1] => abcc [2] => c [3] => e)
(? P Pattern)

Although this method looks a little more complex when constructing a regular expression, it is essentially the same as (pattern. The biggest advantage lies in the processing of results. programmers can directly set You can call the results directly without having to count the desired results in the subgroups.


The code is as follows:


$ Regex = '/(? P \ W (? P \ W) abc (? P \ W) 45 /';

$ Str = 'fsab45 ';
$ Matches = array ();

If (preg_match ($ regex, $ str, $ matches )){
Print_r ($ matches );
}


Matching result:

The code is as follows:


Array ([0] => fsab45[ group1] => fs [1] => fs [group2] => s [2] => s [group3] => d [3] => d)
\ Num

Num is an integer that is a reverse reference to the capture group. For example, \ 2 indicates the matching value of the second sub-group and \ indicates the matching value of the first sub-group.


The code is as follows:


$ Regex = '/(\ w) \ 2 \ 1 /';
$ Str = 'ABBA ';
$ Matches = array ();

If (preg_match ($ regex, $ str, $ matches )){
Print_r ($ matches );
}

Matching result:

The code is as follows:


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


Note: I neglected a small detail here. at first, my first code was $ Regex = "/(\ w) \ 2 \ 1 /";No matching results are returned. After debugging, you can only use ''here ''. 'And "usage difference you still need to pay attention.

 \ K <name>


Understand (? P Pattern) and \ num. \ K <name> is a reverse reference to the named capture group. The name is the name of the capture group.


The code is as follows:


$ Regex = '/(? P \ W) abc \ k /';

$ Str = "fabcf ";

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

Print_r ($ matches );

Matching result:

The code is as follows:


Array ([0] => Array ([0] => fabcf) [name] => Array ([0] => f) [1] => Array ([0] => f ))

Non-capturing group:

(? : Pattern)

The only difference between pattern and pattern is that pattern is matched but the matching result is not captured. Here, we will not give an example.

There are also four ways to actually talk about one thing: Pre-query.

The pre-check score is forward and reverse pre-check. Based on the literal understanding, forward pre-query determines whether or not some characters after the matching string exist, while reverse pre-query determines whether or not some characters before the matching string exist.

Positive pre-query judgment is used (? = Pattern) to determine whether a request does not exist (?! Pattern ).

Reverse pre-check to determine whether there is a use (? <= Pattern) to determine whether or not to use (?

The code is as follows:


$ Regx = '/(? <= A) bc (? = D )/';

$ Str = "abcd ebcd abce ebca ";

If (preg_match_all ($ regx, $ str, $ matches )){

Print_r ($ matches );
}

Matching result:

The code is as follows:


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


If you pay attention to the position of the relatively matched string and whether the assertion must be negative, you will soon be able to master these four forms.

In addition, the four pre-check methods are zero-width. when matching, only one judgment is performed, which is not in the position. /HE (? = L) LLO/matches with HELLO, while/HE (? = L) LO/does not match HELLO. After all, the two do not match in terms of the number of bytes. The former has only four, while the latter has five.

The above is the full description of the regular expression capturing group and non-capturing group in PHP. I hope you will be enlightened.

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.