0

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.

David Rodrigues
  • 10,690
  • 14
  • 51
  • 83
  • 3
    Have you tried a search for "match balanced parenthesis"? – Cupcake Protocol Sep 06 '18 at 21:53
  • Thanks. I did a search, but not with your term that seems fits what I searching for. I will close that as duplicate. – David Rodrigues Sep 06 '18 at 21:56
  • I ran into problems doing basically exactly this and asked a question on it. I'm not sure PCREs are actually perl-compatible for this feature, though. If they are, this will probably be very useful: https://stackoverflow.com/questions/49439805/perl-balanced-constructs – zzxyz Sep 06 '18 at 22:09
  • Actually you can do what you want with PCRE https://stackoverflow.com/questions/51911336/nested-regex-capture/51911406#51911406 – CrafterKolyan Sep 06 '18 at 22:11
  • @zzxyz PCRE is Perl Compatible Regular Expression) – CrafterKolyan Sep 06 '18 at 22:13
  • @CrafterKolyan - Yeah, but I've run into cases where they're not :) – zzxyz Sep 06 '18 at 22:19
  • This quetion has been marked as duplicate, yet in the duplicate, i do not see the solution::they say that regular expressions can not be used to match parantheses!!! this is a lie!!! check [here](https://regex101.com/r/Eh6o8D/2) – Onyambu Sep 06 '18 at 23:22
  • 1
    This quetion has been marked as duplicate, yet in the duplicate, i do not see the solution::they say that regular expressions can not be used to match parantheses!!! this is a lie!!! check [here](https://regex101.com/r/Eh6o8D/7). I have given you a regular expression that can be used to determine completion!!! – Onyambu Sep 06 '18 at 23:43

0 Answers0