Number of capture groups in a regular expression in php I am trying to find the number of capture groups for a regular expression using this PHP function.

/**
 * Finds the number of groups in a regular expression
 * @param $regexPatten string The regular expression
 * @return int Returns the number of groups inside the regular expression
 */
public static function numOfGroups($regexPatten)
{
    $regexPatten = str_replace('\\\\', "", $regexPatten); // remove all escaped backslashes
    $regexPatten = str_replace('\\(', "", $regexPatten); // remove all escaped open parentheses
    $regexPatten = str_replace('(?:', "", $regexPatten); // remove all none capture groups

    return substr_count($regexPatten, "("); // count the remaining opening parentheses
}


And here is my test:

var_dump(Parser::numOfGroups("(test)")); // 1  --  will match: test
var_dump(Parser::numOfGroups("\((test)\)")); // 1  --  will match: (test)
var_dump(Parser::numOfGroups("\\\\\((test)\\\\(\d+)\)")); // 2  --  plain regex: \\\((test)\\(\d+)\)  --  will match: \(test\343)
var_dump(Parser::numOfGroups("((?:test(?: )?)+)")); // 1  --  will match: test test


Is there any use cases I have forgotten?
You might consider non-capturing groups 

(?:/*pattern*/)


These will match your regex but won't normally be operated on in whatever code is running the regex you're evaluating.