protected function getNumberOfCapturingGroupsInRegex($regex)
{
// The regex to count the number of capturing groups should be fairly complete. The only thing I know it
// won't work with are (?| ... ) groups.
return preg_match_all('~
(?:
\\(\\?\\(
| \\[ [^\\]\\\\]* (?: \\\\ . [^\\]\\\\]* )* \\]
| \\\\ .
) (*SKIP)(*FAIL) |
\\(
(?!
\\?
(?!
<(?![!=])
| P<
| \'
)
| \\*
)
~x', $regex, $dummyVar);
}