Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to match nested function invocations (bracket pairs) using a regular expression (recursive?)

I'm looking for a regular expression (**) that will match an unknown number of nested functions. So

expression
function(expression)
function(function(expression))
function(function(function(expression)))
etc.

will all match successfully. But for instance if I add an extra closing bracket at the end it wouldn't be included in the match.

(**) Please don't answer that this would be easier to do by parsing (and counting brackets) rather than using a regular expression - after scratching my head for a while I know that already!

like image 811
Steve Chambers Avatar asked Dec 26 '22 17:12

Steve Chambers


2 Answers

I'm looking for a regular expression (**) that will match an unknown number of nested functions.

Some regex implementations support recursive matching (Perl, PHP, .NET), but JavaScript does not. So, the answer to your question is: no, that is not possible.

like image 60
Bart Kiers Avatar answered Dec 29 '22 07:12

Bart Kiers


This isn't recursive, but it does the trick.

PRECONDITIONS:

  • Function names are alpha-numeric/underscore with possible leading white space.
  • Function names do not start with a number.
  • Expressions have no parentheses in them.
  • There is only one expression nested within the functions.

CODE:

var target = "function(function(function(expression)))";
var pattern = /\s*([a-zA-Z_]\w*[(](\s*[a-zA-Z_]\w*[(]|[^()]+[)]|[)])+[)])/;
var matches = target.match(pattern);
var target= matches[1];

REGULAR EXPRESSION DISSECTION:

\s*             // 0+ white space characters
(               // Capture group for what you want
  [a-zA-Z_]     //   1 letter/underscore
  \w*           //   0+ word characters (alpha-numeric/underscore)
  [(]           //   left parenthesis
  (             //   PIECES:
    \s*         //     0+ white space characters
    [a-zA-Z_]   //     1 letter/underscore
    \w*         //     0+ word characters (alpha-numeric/underscore)
    [(]         //     left parenthesis
   |            //   OR
    [^()]+      //     1+ non-parenthesis characters
    [)]         //     right parenthesis
   |            //   OR
    [)]         //     right parenthesis
  )+            //   1+ of these PIECES
  [)]           //   right parenthesis
)
like image 26
NoBrainer Avatar answered Dec 29 '22 07:12

NoBrainer