Php Regular Expression capture group and non-capture group, php Regular Expression

Source: Internet
Author: User
Tags php regular expression

Php Regular Expression capture group and non-capture group, php Regular Expression

Familiar with regular expressions is the basic requirement of every programmer. For every beginner, the regular expression will be dazzled by a series of characters. This is the case for bloggers who have been inexplicably afraid of regular expressions. Recently, I have seen another blogger write the article "php regular expression", which has benefited a lot from it.WildcardAndCapture DataTwo chapters are quite interesting. The two chapters cover the knowledge of capturing groups and non-capturing groups of regular expressions. Therefore, this article will discuss this knowledge in detail.

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

$regex = '/(ab(c)+)+d(e)?/';    $str = 'abccde';$matches = array(); if(preg_match($regex, $str, $matches)){    print_r($matches);}

Matching result:

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

(? P <Name>Pattern)

Although this method looks a little more complex when constructing a regular expression, it is essentially (Pattern. The biggest advantage lies in the processing of results. programmers can directly call the results based on their own <name>, instead of having to count the desired results in the 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 result:

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 capture group. For example, \ 2 indicates the matching value of the second sub-group and \ indicates the matching value of the first sub-group.

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

Matching result:

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 <Name>Pattern) And \Num,This is easy to understand. \ K <name> is a reverse reference to the named capture group. The name is the name of the capture group.

$regex='/(?P<name>\w)abc\k<name>/';$str="fabcf";echo preg_match_all($regex, $str,$matches);print_r($matches);

Matching result:

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

 

  Non-capturing Group:

(? :Pattern)

And (PatternThe only difference is that pattern is matched but the matching result is not captured. Here, we will not give an example.

  

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

The pre-check score is forward and reverse pre-check. Based on the literal understanding, forward pre-query is used to judge matching strings.BackSome Characters exist or not, while reverse pre-query determines matching stringsFrontWhether or not some characters exist.

Use of forward pre-check(? =Pattern), To identify that the use does not exist(?!Pattern).

Used for reverse pre-check(? <=Pattern), To identify that the use does not exist(? <!Pattern).

$regx='/(?<=a)bc(?=d)/';$str="abcd ebcd abce ebca";if(preg_match_all($regx, $str, $matches)){    print_r($matches);}

Matching result:

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.

 

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.