PHP Regular Expression capture group and non-capturing group, PHP regular Expression _php tutorial

Source: Internet
Author: User
Tags php regular expression

PHP Regular Expression capturing group and non-capturing group, PHP regular expression


Mastering regular expressions is a basic requirement for every programmer, and for each beginner, a string of characters from the regular expression will get dizzy. Bloggers will be so, has been the regular expression of a kind of inexplicable fear. The recent reading of the PHP regular expression, written by another blogger, has gained a lot of interest in its wildcard characters and two chapters capturing data . These two chapters are related to the knowledge of the capturing group and the non-capturing group of the regular expression, so this paper discusses this part of the knowledge carefully.

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 direct quick call results, without having to go to the number of required results in the first few subgroups.

$regex = '/(? P
 
  
   
  \w (? P
  
   
    
   \w)) ABC (? P
   
    
     
    \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
 
  
   
  \w) abc\k
  
   
    
   /'
   
     ;
   
    $str= "FABCF"
   
    ;
   
    Echo 
   
    Preg_match_all (
   
    $regex, 
   
    $str,
   
    $matches
   
    );
   
    Print_r (
   
    $matches);
  
   
 
  

Matching results:

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

  non-capturing group :

(?:pattern)

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 (?! 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 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.

http://www.bkjia.com/PHPjc/1040161.html www.bkjia.com true http://www.bkjia.com/PHPjc/1040161.html techarticle PHP Regular Expression capturing group and non-capturing group, PHP regular expression proficiency in regular expression is the basic requirement of each programmer, for each beginner will be a regular expression ...

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