PCRE/Perl regex, but I'm just guessing what you want (depending on the specs below):
/(?:function\s+|\G(?<!^),\s*)([^,{}]+|\{function\s+(?1)\s*(?:,\s*(?1)\s*)*})\s*/g
Example Perl use:
test('function {function arg1, arg2}, arg3');
test('function foo, {function {function x}, y}, bar');
sub test{
print 'Matched: "', join '", "',
$_[0] =~
/(?:function\s+|\G(?<!^),\s*)
(
[^,{}]+
|
\{function\s+(?1)\s*(?:,\s*(?1)\s*)*}
)
\s*/gx;
print "\"\n";
}
Output:
Matched: "{function arg1, arg2}", "arg3"
Matched: "foo", "{function {function x}, y}", "bar"
Should work just fine in PHP, just escape where needed and use with preg_match_all.
Lots of questions about the specification tho:
- Does the string you are matching on contain anything else than this?
- Are there always two arguments?
- Are arguments only one word?
- Is "function" a keyword or a function name?
More (real) examples please!