I am having trouble to evaluate the following expression using RegEx in C#.
add(1, 2, sub(4, add(1, 2)), div(4, 2))
which will be evaluated as below.
=> add(1, 2, sub(4, 3), 2)
=> add(1, 2, 1, 2)
=> 6
Functions are picked up from regex expression and parameters are of any numbers. Thanks in advance.
Here is what I am trying:
class Program
{
static Regex extractFuncRegex = new Regex(@"(?<func>add|sub|div)\s*\((?<params>.*)\)$", RegexOptions.ExplicitCapture);
static Regex extractArgsRegex = new Regex(@"([^,]+\(.+?\))|([^,]+)");
static void Main(string[] args)
{
string test = @"add(1, 2, sub(4, add(1, 2)), div(4, 2))";
Console.WriteLine(ParseFunction(test));
Console.ReadLine();
}
static string ParseFunction(string expr)
{
expr = extractFuncRegex.Replace(expr, (m) =>
{
string func = m.Groups["func"].Value;
string param = m.Groups["params"].Value;
Console.WriteLine("Function: {0}", func);
MatchCollection paramCollection = extractArgsRegex.Matches(param);
List<string> pa = new List<string>();
foreach (Match item in paramCollection)
{
string p = item.Groups[0].Value.Trim();
Console.WriteLine("\tParameter: {0}", p);
if (extractFuncRegex.IsMatch(p))
p = ParseFunction(p);
pa.Add(p);
}
switch (func)
{
case "add":
float a1 = 0;
foreach (string item in pa)
a1 += float.Parse(item);
return a1.ToString();
case "sub":
return (float.Parse(pa[0]) - float.Parse(pa[1])).ToString();
case "div":
return (float.Parse(pa[0]) / float.Parse(pa[1])).ToString();
default:
return expr;
}
});
return expr;
}
}
If you debug, you can see, there is a problem to parse
sub(4, add(1, 2))