Question about regular expression capturing? $ Str = "a1234a"; $ preg = "/(\ d) {4}/I"; the above can match four consecutive numbers, in this case, the subexpression captures 1 (a subexpression )? Or 1324 (a subexpression )? Or 1, 2, 3, and 4 (four subexpressions )? Question about regular expression capturing?
$ Str = "a1234a ";
$ Preg = "/(\ d) {4}/I ";
The above can match four consecutive numbers,
Then the sub-expressions in the capture
Is 1 (a subexpression )?
Or 1324 (a subexpression )?
Or 1, 2, 3, and 4 (four subexpressions )? Share:
------ Solution --------------------
Let's take a look.
$str = "a1234a";
$preg = "/(\d){4}/i";
preg_match_all($preg, $str, $res);
print_r($res);
Array
(
[0] => Array
(
[0] = & gt; 1234
)
[1] => Array
(
[0] => 4
)
)
------ Solution --------------------
Reference:
$ Str = "a1234a ";
$ Preg = "/(\ d) {4}/I ";
The above can match four consecutive numbers,
Then the sub-expressions in the capture
Is 1 (a subexpression )?
Or 1324 (a subexpression )?
Or 1, 2, 3, and 4 (four subexpressions )?
Generally, the first value is the part obtained by the entire expression.
Then there is a bracket value.
Just like your regular expression
The first value is 1234
The second value is: 4.
If $ arr is used as an array
$ Arr [0] = 1234
$ Arr [1] = 4
------ Solution --------------------
The (\ d) {4} you used indicates that a single number character is a sub-group and repeated four times. Therefore, after execution, it should be
\ D Match 1 sub-group as 1
\ D matches 2 with the same sub-group and overwrites the previous value. The sub-group is 2.
\ D matches 3 with the same sub-group, overwrites the previous value, and the sub-group is 3
\ D matches 4 with the same sub-group and overwrites the previous value. The sub-group is 4.
If it is (\ d {4}), it is obviously different from the previous, meaning that four consecutive numeric characters are child groups.