\$\begingroup\$
\$\endgroup\$
13
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: \(test343円)
var_dump(Parser::numOfGroups("((?:test(?: )?)+)")); // 1 -- will match: test test
Is there any use cases I have forgotten?
200_success
145k22 gold badges190 silver badges478 bronze badges
1 Answer 1
\$\begingroup\$
\$\endgroup\$
3
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.
-
\$\begingroup\$ This is not a bad idea, but the function is part of a router, and I can imagine people who use the router are going to forget this :( \$\endgroup\$Benjaco– Benjaco2017年06月28日 20:44:51 +00:00Commented Jun 28, 2017 at 20:44
-
\$\begingroup\$ but I have forgotten to remove non-capturing groups, thank you for that insight \$\endgroup\$Benjaco– Benjaco2017年06月28日 21:32:20 +00:00Commented Jun 28, 2017 at 21:32
-
\$\begingroup\$ I will force the developers to use none capture groups \$\endgroup\$Benjaco– Benjaco2017年06月30日 11:15:46 +00:00Commented Jun 30, 2017 at 11:15
lang-php
[...(...]
come to mind. \$\endgroup\$(
as a capturing one. \$\endgroup\$