Look this example:
function(abc, innerFunction(1, 2), innerFunction(3, 4, 5))
│ │ │ │ ││
│ └────┘ └───────┘│
└────────────────────────────────────────────────┘
↑ ↑
Open Close
I need to match entire (outter) function()
. For that I need count how much (
has and stop after found the same number of )
. It normally needs a hardcoded script to possibility it (a parser), but because Regular Expression (in my case, PCRE) is very flexible, maybe it could be possible in anyway.
This is my current solution that matches only outter functions: https://regex101.com/r/Eh6o8D/1
Due to my real objective, it is almost certain that I will do it via hardcode. But I decided post it because it could be useful to another devs, and it could be used in another contexts on my app too.
Edit 1: this question is duplicated, but we can learn here the following: (?R)
is a special feature of PCRE that will recurse entire Regular Expression in this point. So if we have /ab(?R)?/
it will be similar to /abababab.../
infinitely.
In case you need just recurse a specific match group, then you could use \g<n>
(where n
is the match group number) or \g<group>
(where group
is the match group name). So if we have /(ab)\g<1>+c/
it will be limitar to /abababab...c/
.
The (?R)
could solve my problem here. But it will only grab entire match, so I still need process that to get each of parameters, for instance.
(?<function>\w+) \( # Matches function name
(?<parameters> # Simple param type.
(?<data> # Simple data type: inner function or word (keep order!).
(?R) | # Or recursive this Regexp to match inner functions.
\w+ # Matches a simple word.
)
( # Match additional parameters:
\s*,\s* # Match comma, but skip spaces.
\g<parameters> # Recurse additional parameters.
)* # Zero or more additional parameters.
)? # Parameters are optional
\)
It will solve my problem by grouping function name and parameters as you can see here https://regex101.com/r/uyq6yM/1, capturing inner functions as parameter that I could work in another moment. It still can be improved, but for now, it is a acceptable solution for this case.