Analysis of capturing group and non-capturing group instances in PHP regular

Source: Internet
Author: User
This article mainly introduces the capture group and the non-capturing group instance analysis in the PHP regular, the interest friend's reference, hoped to be helpful to everybody.

What is a capture group

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

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 match pattern, $string is the string to match.

Array & $match, which is an array of,& that matches the results will be written to $match.

int $flags If this token is passed, a string offset (relative to the target string) is appended to each occurrence of the match returned.

The int $offset is used to specify the start of a search (in bytes) from an unknown target string.

Let's take a look at what's in $match's value:

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

The results are as follows:

Array (size=4) = String ' a=4b=98c=56 ' (length=11) and String ' 4 ' (length=1) = String ' 98 ' (length=2) = String ' 5 6 ' (length=2)

Now that we know what a capture group is, the capturing group is the part of a regular expression enclosed in (), and each pair () is a capturing group.

PHP will number it, starting from 1. As for why it starts with 1, that's because PHP matches the full string number to 0.

If you have multiple parentheses or nested parentheses, number them in the order they appear in the left parenthesis ,

When matched by matching pattern in the figure, the 123th numbers of the capturing group are red, green, and blue respectively.

Ignore and name a capturing group

We can also prevent PHP from matching the group's number: in the matching group before the mode plus?:

$mode = '/a= (\d+) b= (?: \ d+) c= (\d+)/';

In this way, the matching result becomes:

Array (size=3) = String ' a=4b=98c=56 ' (length=11) and String ' 4 ' (length=1) = String ' "(length=2)

Of course, we can also give it a unique name within the parentheses.

Named subgroups can be accepted (?<name>), (? ' Name ') and (? p<name>) syntax. Previous versions only accepted (? p<name>) syntax.

For example: $mode = '/a= (\d+) b= (? p<sec>\d+) c= (\d+)/';

When used, the result is:

Array (size=5) = = String ' a=4b=98c=56 ' (length=11) = String ' 4 ' (length=1) ' sec ' = = String ' 98 ' (length=2) = St Ring ' 98 ' (length=2) = String ' "(length=2)

When you keep an indexed array, plus an associated item, the key value is the capturing group name.

Reverse reference of capturing group

When we use the Preg_replace () function for regular substitution, we can also use \ n or $n to refer to the nth capturing 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 represents capture Group 1 (4), which is the Capture Group 2 (98), and \3 is capturing Group 3 (56).

Use of non-capturing groups:

Why is it called a non-capturing group? That's because they have the characteristics of capturing groups, in matching pattern (), but when matched, PHP does not group them, they only affect the matching results and are not output as results.

The /d (? =xxx) match "is followed by a number of XXX."

Note format: Can only be placed after the matching pattern string!

For example:

$pattern = '/\d (=abc)/'; $str = "Ab36abc8eg"; $res =preg_match ($pattern, $str, $match); Var_dump ($match);//6

Match 6 because it is only a number, followed by ABC.

(? <=xxx) / D Match " front is a number of xxx"

Note format: Can only be placed before the matching pattern string!

For example:

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

Match 8 because it is only a number, followed by ABC.

The opposite of (? =xxx) (? <=xxx) is (?! =xxx) (? <!=xxx) They added non-operator "!" in front of =

It represents a string that is not preceded/followed by XXX and is no longer an example.

Summary: The above is the entire content of this article, I hope to be able to help you learn.

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.