Data:
today 13/9 : Aristides, Aristeides, Aristeidis, Kornelios, Kornilios, Kornelia (source : www.namedays.gr)
Regex pattern:
/(?:today \d{1,2}\/\d{1,2}\s\:\s)([\w[:blank:],]+)(?:\s\(source\s\:\swww\.namedays\.gr\))/
Result:
Array
(
[0] => today 13/9 : Aristides, Aristeides, Aristeidis, Kornelios, Kornilios, Kornelia (source : www.namedays.gr)
[1] => Aristides, Aristeides, Aristeidis, Kornelios, Kornilios, Kornelia
)
My question is, why my pattern not return only with the names -> Array[1] and why including the non-capturing groups in my result Array[0] also?
I can not figure it out. I'm expecting only Array[1].
The 0
th capturing group always contains the entire matched text. The first capturing group is always $1
(or a named).
If it's a problem, you can always unset()
it.
Everything is correct. 0-th group is always the entire match, regardless of groups, and the 1st group is the first capturing group result.
E.g., see preg_match doc,
If matches is provided, then it is filled with the results of search. $matches[0] will contain the text that matched the full pattern, $matches[1] will have the text that matched the first captured parenthesized subpattern, and so on.