admin管理员组

文章数量:1279148

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!

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!

Share Improve this question edited Nov 9, 2016 at 16:10 Steve Chambers asked Sep 25, 2012 at 13:35 Steve ChambersSteve Chambers 39.5k29 gold badges176 silver badges220 bronze badges 5
  • 1 This is not a regular language, so there exists no RE that can recognize it. – Fred Foo Commented Sep 25, 2012 at 13:36
  • 3 This would be easier to do by parsing (and counting brackets) rather than using a regular expression – hsz Commented Sep 25, 2012 at 13:36
  • 4 @larsmans, while you're correct that a formal regular expression cannot represent this, there are indeed regex dialects that allow for some recursion support, such as PCRE or the .NET engine with its balancing groups. Either way, a proper parser should be used here, and the regex only for tokenization. – Lucero Commented Sep 25, 2012 at 13:39
  • @SteveChambers: can you tell us why you'd want an RE for this problem if a simple stack-based parser can solve it? – Fred Foo Commented Sep 25, 2012 at 13:51
  • @larsmans Thanks for the q. This is being done in Javascript and have already gone way too far down the route of regular expressions to change that. If it's not possible using regular expressions (which seems the case based on the answers and negging) I'll just have to go with an inferior regex that doesn't really match what it's supposed to... – Steve Chambers Commented Sep 25, 2012 at 14:08
Add a ment  | 

3 Answers 3

Reset to default 6

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
)

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.

Worth noting as per Bart Kiers's answer that some regex engines (excluding Javascript) have extended features to provide recursive matching - but such a feature shouldn't be considered to be a regular expression as per the formal definition.

本文标签: