I found the following answer on stackoverflow, in which it is described how to use a regex pattern to extract (nested) functions and arguments from a string.
The user also provides an example for which the pattern should work, and after he extracts the arguments from the most-outer function (which works fine in php too), he matches the following text
a,b,func1(a,b+c),func2(a*b,func3(a+b,c)),func4(e)+func5(f),func6(func7(g,h)+func8(i,(a)=>a+2)),g+2
with the following pattern
(?:[^,()]+((?:\((?>[^()]+|\((?<open>)|\)(?<-open>))*(?(open)(?!))\)))*)+
The following (simpler) pattern is also supposed to work
(?:[^,()]+((?:\((?>[^()]+|\((?<open>)|\)(?<-open>))*\)))*)+
Note that the user was using C# for his example, and I want to get it to work in PHP, however I can't manage to do it.
So when I try to match the example String using preg_match($pattern, $string, $matches)
, it doesn't return any matches.
If I enter the pattern and the example on regex101, it's giving me errors and I can't fix them (I guess it has something to do with the <open>
part of the pattern).
I'm not the best in regex, so can someone please help me out and tell me what I need to change in the pattern for it to work with php?