Talking about the capturing group and non-capturing group in PHP regular expressions, talking about the php capturing Group
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)=> string 'a=4b=98c=56' (length=11)=> string '4' (length=1)=> string '98' (length=2)=> 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)=> string 'a=4b=98c=56' (length=11)=> string '4' (length=1)=> string '56' (length=2)
Of course, we can also give it a unique name inside the brackets.
The naming sub-group is acceptable (? <Name> ),(? 'Name') and (? P <name>) syntax. Earlier versions only accept (? P <name>) syntax.
For example: $ mode = '/a = (\ d +) B = (? P <sec> \ d +) c = (\ d + )/';
The result is as follows:
array (size=5)=> string 'a=4b=98c=56' (length=11)=> string '4' (length=1) 'sec' => string '98' (length=2)=> string '98' (length=2)=> 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 )(? <! = Xxx) they add the non-operator "!" before =.
It indicates the front/backNoXxx string.
The above discussion about the capture group and non-capture group in PHP regular expressions is all the content shared by the editor. I hope you can give us a reference and support for the group.