Matching:
When engine matches a part of string or the whole but does return nothing.
Capturing:
When engine matches a part of string or the whole and does return something.
--
What's the meaning of returning?
When you need to check/store/validate/work/love a part of string that your regex matched it before you need capturing groups (...)
At your example this regex .*?d+
just matches the dates and years See here
And this regex .*?(d+)
matches the whole and captures the year See here
And (.*?(d+))
will match the whole and capture the whole and the year respectively See here
*Please notice the bottom right box titled Match groups
So returning....
1:
preg_match("/.*?d+/", "Jan 1987", $match);
print_r($match);
Output:
Array
(
[0] => Jan 1987
)
2:
preg_match("/(.*?d+)/", "Jan 1987", $match);
print_r($match);
Output:
Array
(
[0] => Jan 1987
[1] => Jan 1987
)
3:
preg_match("/(.*?(d+))/", "Jan 1987", $match);
print_r($match);
Output:
Array
(
[0] => Jan 1987
[1] => Jan 1987
[2] => 1987
)
So as you can see at the last example, we have 2 capturing groups indexed at 1 and 2 in the array, and 0 is always the matched string however it's not captured.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…