How to tell if PREG named groups were used in the pattern passed to preg_match()?



Dear Overflowers,

I'm interested in determining if named groups were used in the pattern passed to preg_match().

Imagine a scenario in which a list of regex patterns are iterated over and passed into preg_match(). Something like the following:

$trg = "123abc/4";
$patterns = array('/abc/', '/abc\/(\d+)/', '/abc\/(?P<id>\d+)/');
foreach ($patterns as $p) {
preg_match($p, $trg, $matches);
if (len($matches) > 0) {
// Do something interesting with the capture
}
}

If a match is found, then there will be at least one element in $matches. The two final patterns contain a capture, but $matches will be a two element array in the first case and
a three element array in the last.

I want to know, without grepping the pattern, if named groups were used. I need to know this because I want to pass the captured text on to other functions.

As you can imagine, the patterns will not be known until runtime, so I can't simply look at the number of elements in the match.

Any ideas on how to tackle this?

Thanks for your time.