Capture Group and non-capture group in PHP regular expressions

Source: Internet
Author: User
The capturing group and non-capturing group in PHP regular expressions encountered a regular expression matching problem today. suddenly, the concept of a capturing group exists, and the manual is also a little over, baidu has no intention of turning to the special usage of regular expression capturing groups in C # and Java. when the search keyword is PHP, there is no relevant content. I tried it myself and found that it is also feasible in PHP, to sum up, I also hope that students with great knowledge and attention can find problems in my understanding. & Nbsp; what is a capturing group? let's take a look at the capturing group and non-capturing group in PHP regular expressions.

Today, I encountered a regular expression matching problem and suddenly turned to the concept of a capturing group. The manual was a little over, baidu has no intention of turning to the special usage of regular expression capturing groups in C # and Java. when the search keyword is PHP, there is no relevant content. I tried it myself and found that it is also feasible in PHP, to sum up, I also hope that students with great knowledge and attention can find problems in my understanding.

What is a capture group?

Let's take a look at PHP's regular expression matching function.

Int preg_match (string $ pattern, string $ subject [, array & $ matches [, int $ flags = 0 [, int $ offset = 0])

The first two items are commonly used. $ pattern is the regular expression matching mode, and $ string is the string to be matched.

Array & $ match, which is an array, & indicates that the matched results will be written into $ match.

If int $ flags passes this flag, a string offset (relative to the target string) will be appended to each occurrence of the matching result ).

Int $ offset is used to specify to start searching from an unknown target string (in bytes ).

Let's take a look at the values of $ match:

$mode = '/a=(\d+)b=(\d+)c=(\d+)/';$str='**a=4b=98c=56**';$res=preg_match($mode,$str,$match);var_dump($match);

The result is as follows:

array (size=4)  0 => string 'a=4b=98c=56' (length=11)  1 => string '4' (length=1)  2 => string '98' (length=2)  3 => string '56' (length=2)

Now we know what a capturing group is. a capturing Group is a part enclosed by () in a regular expression. each pair () is a capturing group.

PHP will number it, starting from 1. As for why it starts from 1, PHP numbers the matched complete string to 0.

If there are multiple parentheses or nested parentheses, pressSequence of left parenthesesTo number,

When matching in the matching mode shown in the figure, the number 123 of the capture group is red, green, and blue.

Ignore and name a capture group

We can also prevent PHP from adding the matching group number before the matching group mode.? :

$ Mode = '/a = (\ d +) B = (? : \ D +) c = (\ d + )/';

In this way, the matching result will become:

array (size=3)  0 => string 'a=4b=98c=56' (length=11)  1 => string '4' (length=1)  2 => string '56' (length=2)

Of course, we can also give it a unique name inside the brackets.

The naming sub-group is acceptable (? ),(? 'Name') and (? P ) Syntax. earlier versions only accept (? P ) Syntax.

For example:$ Mode = '/a = (\ d +) B = (? P \ D +) c = (\ d + )/';

The result is as follows:

array (size=5)  0 => string 'a=4b=98c=56' (length=11)  1 => string '4' (length=1)  'sec' => string '98' (length=2)  2 => string '98' (length=2)  3 => string '56' (length=2)

Add an association item while retaining the index array. The key value is the name of the capture group.

Reverse reference of a capture group

When we use the preg_replace () function for regular expression replacement, we can also use\ NOr$ NTo reference the nth capture group.

$mode = '/a=(\d+)b=(\d+)c=(\d+)/';$str='**a=4b=98c=56**';$rp='\1/$2/\3/';echo preg_replace($mode,$rp,$str);//**4/98/56/**

\ 1 indicates capture group 1 (4), $2 is capture group 2 (98), and \ 3 is capture group 3 (56 ).

Non-capturing group usage:

Why is it a non-capturing group? That's because they have the capture group feature. in the matching mode (), but in the matching mode, PHP will not group them. they will only affect the matching results and will not serve as the result output.

/D (? = Xxx)Match"BackIs a number of xxx ".

Note format: only strings in matching mode can be placed.After!

For example:

$pattern='/\d(?=abc)/';$str="ab36abc8eg";$res=preg_match($pattern,$str,$match);var_dump($match);//6

Match 6 because it is only used as a number, followed by abc.

(? <= Xxx)/D Match"FrontIs a number of xxx"

Note format: only strings in matching mode can be placed.Before!

For example:

$pattern='/(?<=abc)\d/';$str="ab36abc8eg";$res=preg_match($pattern,$str,$match);var_dump($match);//8

Match 8 because it is only used as a number, followed by abc.

And (? = Xxx )(? <= Xxx) is relative (?! = Xxx )(? !"

It indicates the front/backNoXxx string.

If you think this blog is helpful to you, you can recommend or follow me. if you have any questions, leave a message below to discuss them. thank 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.