Php regular expression capturing group and non-capturing group

Source: Internet
Author: User
Tags php regular expression
: This article mainly introduces the php regular expression capturing group and non-capturing group. if you are interested in the PHP Tutorial, refer to it. 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 blog member write "php regular expression", which has benefited a lot and is interested in its wildcards and data capturing chapters. 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 set You can call the results directly without having to count the desired results in the subgroups.

$regex = '/(?P
 
  \w(?P
  
   \w))abc(?P
   
    \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
 
  \w)abc\k
  
   /';$str="fabcf";echopreg_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 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 the application does not exist (?!Pattern).

Reverse pre-check to determine whether there is a use (? <=Pattern) To determine whether the application 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.

The above introduces the php regular expression capturing group and non-capturing group, including the content, hope to be helpful to friends who are interested in the PHP Tutorial.

Related Article

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.