This article mainly introduces the regular expression capturing group and non-capturing group in php. If you need a regular expression, you can refer to the regular expression frequently used during project development, it can be said that regular expressions are the most basic requirement of every programmer. It is difficult for beginners to get started with regular expressions. Recently, I saw a friend's blog writing "PHP regular expression", which has benefited a lot. In this section, I am very interested in wildcards and data capturing. These two chapters also involve the capture group and non-capture group content of the regular expression, so as to analyze this content.
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 '.
The Code is as follows:
$ Regex = '/(AB (c) + d (e )? /';
$ Str = 'abcde ';
$ Matches = array ();
If (preg_match ($ regex, $ str, $ matches )){
Print_r ($ matches );
}
Matching result:
The Code is as follows:
Array ([0] => abccde [1] => abcc [2] => c [3] => e)
(? P Pattern)
Although this method looks a little more complex when constructing a regular expression, it is essentially the same as (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.
The Code is as follows:
$ Regex = '/(? P \ W (? P \ W) abc (? P \ W) 45 /';
$ Str = 'fsab45 ';
$ Matches = array ();
If (preg_match ($ regex, $ str, $ matches )){
Print_r ($ matches );
}
Matching result:
The Code is as follows:
Array ([0] => fsab45[ 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.
The Code is as follows:
$ Regex = '/(\ w) \ 2 \ 1 /';
$ Str = 'abba ';
$ Matches = array ();
If (preg_match ($ regex, $ str, $ matches )){
Print_r ($ matches );
}
Matching result:
The Code is as follows:
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 Pattern) and \ num. \ K <name> is a reverse reference to the named capture group. The name is the name of the capture group.
The Code is as follows:
$ Regex = '/(? P \ W) abc \ k /';
$ Str = "fabcf ";
Echo preg_match_all ($ regex, $ str, $ matches );
Print_r ($ matches );
Matching result:
The Code is as follows:
Array ([0] => Array ([0] => fabcf) [name] => Array ([0] => f) [1] => Array ([0] => f ))
Non-capturing group:
(? : Pattern)
The only difference between pattern and pattern 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 a request does not exist (?! Pattern ).
Reverse pre-check to determine whether there is a use (? <= Pattern) to determine whether or not to use (?
The Code is as follows:
$ Regx = '/(? <= A) bc (? = D )/';
$ Str = "abcd ebcd abce ebca ";
If (preg_match_all ($ regx, $ str, $ matches )){
Print_r ($ matches );
}
Matching result:
The Code is as follows:
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 is the full description of the regular expression capturing group and non-capturing group in PHP. I hope you will be enlightened.