locked
Help with this regular expression on a math expression

    Question

  • Hi RegExperts

    I need to handle a simple condition ( I think ).

    An opening parentheses indicates either grouping of math expression or the parameter list of a function. I need to detect the grouping parantheses and not the function parentheses.

    Example math expression: 

    ({pi} + 90.88) * ([param1] - [param2.eu]) / (sin(x) * 56)

    3 groups exist: 

    1. ({pi} + 90.88)
    2. ([param1] - [param2.eu])
    3. (sin(x) * 56)

    The 3rd group has a function: sin(x)
    I need to match only the 3 groups opening parentheses while ignoring the function parameter parenthese.

    So my criteria is simply:

    1. If the line starts with a parentheses, it is a group parenthese. IN this case, I do have an opening parentheses starting the expression to demonstrate this functionality.
    2. If the expression contains a (space or open parentheses) followed by an opening parenths, then it's a group.

    The combined criteria can be stated as: 
    If the paranteheses starts the line or if any (white space or parentheses) precedes a parentheses, then it is a group. 

    so: 

    at char 1 is an open parentheses, so it's a group
    at char 18 is an open parentheses, before it is a white space, so it is a group
    at char 45 is an open parentheses, before it is a white space, so it is a group
    at char 49 is an open parentheses, before it is not a white space and is not a parentheses, so it is NOT a group.

    Any pointers or recommendations?

    Thanks
    E*4



    Tuesday, June 17, 2008 4:04 PM

Answers

  • Unfortunately it's not a simple condition.  Regex is great for searching and validation but fails at parsing situations generally because of nesting/grouping.  The .NET regex flavor does provide a feature called balancing groups.  It enables you to ensure the number of opening parentheses match the number of closing ones but beyond that you are out of luck.  What you are really looking for is a parser.  You can find numerous examples with source online from a search for "math expression parser." 
    Tuesday, June 17, 2008 5:58 PM

All replies

  • Unfortunately it's not a simple condition.  Regex is great for searching and validation but fails at parsing situations generally because of nesting/grouping.  The .NET regex flavor does provide a feature called balancing groups.  It enables you to ensure the number of opening parentheses match the number of closing ones but beyond that you are out of luck.  What you are really looking for is a parser.  You can find numerous examples with source online from a search for "math expression parser." 
    Tuesday, June 17, 2008 5:58 PM
  • inetscan said:

    Unfortunately it's not a simple condition.  Regex is great for searching and validation but fails at parsing situations generally because of nesting/grouping.  The .NET regex flavor does provide a feature called balancing groups.  It enables you to ensure the number of opening parentheses match the number of closing ones but beyond that you are out of luck.  What you are really looking for is a parser.  You can find numerous examples with source online from a search for "math expression parser." 



    OK, thanks for your input. I think i'll go ahead and separate (parse) out the subexpressions then evaluate them that way. That way I can continue using Regular Expressions on the various subexpressions.

    Thanks
    E*4 

    Tuesday, June 17, 2008 6:15 PM