6

I am building an application where the user can specify an expression for some fields. The expression shall contain functions too. I need to evaluate these expressions and display final value in report.

I had an expression to extract function-name & it's paramters. Previously, the function parameters were decimal values. But now, the parameters can also be expression.

For ex,

Round( 1  * (1+  1 /100) % (2 -1), 0)

Function-name : Round
Parameter1    : 1  * (1+  1 /100) % (2 -1)
Parameter2    : 0

Previous Regex:

string pattern2 = @"([a-zA-Z]{1,})[[:blank:]]{0,}\(([^\(\)]{0,})\)";

This regex does not help me anymore to find expression-parameters.

Can someone help me with the right regex to extract function-name & parameters? I am implement all or most of the functions supported by Math class. The program is built in c#

Thanks in advance for the help.

1
  • 5
    Expressions with functions are not a regular language, so you can't assume to be able to parse them with regular expressions. Commented Oct 6, 2011 at 20:08

3 Answers 3

3
 "^\s*(\w+)\s*\((.*)\)"

group(1) is function name

split group(2) with "," you get para list.

updated

since I don't have Windows system (.Net either), I test it with python. nested function is not a problem. if we add "^\s*" at the beginning of the expression:

import re

s="Round(floor(1300 + 0.234 - 1.765), 1)"
m=re.match("^\s*(\w+)\s*\((.*)\)",s)
m.group(1)
Output: 'Round'

m.group(2)
Output: 'floor(1300 + 0.234 - 1.765), 1'
you can split if you like:
m.group(2).split(',')[0]
Out: 'floor(1300 + 0.234 - 1.765)'

m.group(2).split(',')[1]                                                                                                        
Out: ' 1'

well, if your function nesting is like f(a(b,c(x,y)),foo, m(j,k(n,o(i,u))) ), my code won't work.

Sign up to request clarification or add additional context in comments.

3 Comments

This won’t work. Consider Round(1) + Round(2) as an example.
well, i thought it would be only one function per line, as example shows.
Expression can have nested functions too. For ex Round(floor(1300 + 0.234 - 1.765), 1)
0

You could try writing a parser, instead of using regular expressions.
The Irony library is (in my opinion) extremely easy to use, and in the examples there's something very similar to what you are trying to do.

Comments

0

From the post Regex for matching Functions and Capturing their Arguments, you can extract your function using Kent regex and use this code to extract parameters form the last group :

string extractArgsRegex = @"(?:[^,()]+((?:\((?>[^()]+|\((?<open>)|\)(?<-open>))*\)))*)+";
var ArgsList = Regex.Matches(m.Groups[m.Groups.Count - 1].Value, extractArgsRegex);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.