Why does this regex capture produce 2 matches?
php, php-5.3, preg-match, regex
Solution
Yes, you get two captures because you have two capturing groups in your regular expression.
To avoid the unwanted capture you could use a non-capturing group `(?:...)`:
/_(\d(?:-\d){0,3})\./
Problem
I do not know why there are 2 matches found aside from the input using this regex, when I expected only 1 match. ``` preg_match(/_(\d(-\d){0,3})\./,$str,$matches); ``` on this file string format `name_A-B-C-D.ext`. I would expect to get a single match like this: ``` Example A [0] => name_A-B-C-D.ext [1] => A-B-C-D Example B [0] => name_A-B-C.ext [1] => A-B-C ``` But this is the result I get: ``` Example A [0] => name_A-B-C-D.ext [1] => A-B-C-D [2] => -D Example B [0] => name_A-B-C.ext [1] => A-B-C [2] => -C ``` I only wish to capture `A` up to `D` if its preceded with a hyphen. This code is usable and I can simply ignore the 2nd match, but I would like to know why its there. I can only assume it has something to do with my two capture groups. Where is my error ?