views:

59

answers:

2

I'm parsing a simple language (Excel formulas) for the functions contained within. A function name must start with any letter, followed by any number of letters/numbers, and ending with an open paren (no spaces in between). For example MyFunc(. The function can contain any arguments, including other functions and must end with a close paren ). Of course, math within parens is allowed =MyFunc((1+1)) and (1+1) shouldn't be detected as a function because it fails the function rule I've just described. My goal is to recognize the highest level function calls in a formula, identify the function name, extract the arguments. With the arguments, I can recursively look for other function calls.

Using this tutorial I hacked up the following regexes. None seem to do the trick. They both fail on test case pasted below.

This should work but completely fails:

(?<name>[a-z][a-z0-9]*\()(?<body>(?>[a-z][a-z0-9]*\((?<DEPTH>)|\)(?<-DEPTH>)|.?)*(?(DEPTH)(?!)))\)

This works for many test cases, but fails for test case below. I don't think it handles nested functions correctly- it just looks for open paren/close paren in the nesting:

(?<name>[a-z][a-z0-9]*\()(?<body>(?>\((?<DEPTH>)|\)(?<-DEPTH>)|.?)*(?(DEPTH)(?!)))\)

Here's the test that breaks them all:

=Date(Year(A$5),Month(A$5),1)-(Weekday(Date(Year(A$5),Month(A$5),1))-1)+{0;1;2;3;4;5}*7+{1,2,3,4,5,6,7}-1

This should be matched as:

Date(ARGUMENTS1)
Weekday(ARGUMENTS2)
Where ARGUMENTS2 = Date(Year(A$5),Month(A$5),1)

Instead it matches:

ARGUMENTS2 = Date(Year(A$5),Month(A$5),1)-1)

I am using .net RegEx which provides for external memory.

A: 

You might want to look at this link: http://www.xtremevbtalk.com/archive/index.php/t-177848.html

Matt Caldwell
@Matt Caldwell - looked at the link but it doesn't resolve my issue. I'm not trying to use Excel to evaluate a function, I'm trying to breakdown larger functions in code. This should be possibly without loading Excel.
SFun28
+1  A: 

This is well within the capabilities of .NET regexes. Here's a working demo:

using System;
using System.Text.RegularExpressions;

namespace Test
{
  class Test
  {
    public static void Main()
    {
      Regex r = new Regex(@"
        (?<name>[a-z][a-z0-9]*\()
          (?<body>
            (?>
               \((?<DEPTH>)
             |
               \)(?<-DEPTH>)
             |
               [^()]+
            )*
            (?(DEPTH)(?!))
          )
        \)", RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);

      string formula = @"=Date(Year(A$5),Month(A$5),1)-(Weekday(Date(Year((A$5+1)),Month(A$5),1))-1)+{0;1;2;3;4;5}*7+{1,2,3,4,5,6,7}-1";

      foreach (Match m in r.Matches(formula))
      {
        Console.WriteLine("{0}\n", m.Value);
      }
    }
  }
}

output:

Date(Year(A$5),Month(A$5),1)

Weekday(Date(Year((A$5+1)),Month(A$5),1))

The main problem with your regex was that you were including the function name as part of the recursive match--for example:

Name1(...Name2(...)...)

Any open-paren that wasn't preceded by name was not counted, because it was matched by the final alternative, |.?), and that threw off the balance with the close-parens. That also meant that you couldn't match formulas like =MyFunc((1+1)), which you mentioned in the text but didn't include in the example. (I threw in an extra set of parens to demonstrate.)

EDIT: Here's the version with support for non-significant, quoted parens:

  Regex r = new Regex(@"
    (?<name>[a-z][a-z0-9]*\()
      (?<body>
        (?>
           \((?<DEPTH>)
         |
           \)(?<-DEPTH>)
         |
           ""[^""]+""
         |
           [^()""]+
        )*
        (?(DEPTH)(?!))
      )
    \)", RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);
Alan Moore
@Alan Moore - Ah! Thanks! I would not have figured that out myself. This is the right solution. Thank you for believing me that this could be done with Regex =) Ok...I'll throw this out there purely as extra-credit/absolutely not needed/still grateful that you unblocked me: is there a way to ignore parens within quotes?
SFun28
@Alan Moore - last comment submitted before complete. So the idea is that =Func("Hel(o") parses fine. I'm throwing this out there because I have a feeling that someone will be asking this very question in the future. For my purposes, I can guarantee that no strings will have unbalanced parens.
SFun28
@SFun28: All you need to do is add another alternative to match complete, quoted sections, and then exclude quotes as well as parens in the final, *everything else* alternative: `(?>\\((?<DEPTH>)|\\)(?<-DEPTH>)|""[^""]+""|[^()""]+)*`
Alan Moore
As such? @"(?<name>[a-z][a-z0-9]*\()(?<body>(?>\((?<DEPTH>)|\)(?<-DEPTH>)|""[^""]+""|[^()""]+)*(?(DEPTH)(?!)))\)"
SFun28
That seems to break some tests (man, i can't press enter for newline in this textbox)
SFun28