I am writing a solve method that solves an equation. The method will be recursive; searching for all outer parenthesis and when found recall solve for values inside the parenthesis and return the value when no parenthesis are found.
The process should look like this
20 * (6+3) / ((4+6)*9)
20 * 9 / ((4+6)*9)
20 * 9 / (10*9)
20 * 9 / 90
2
As you see each match may have a different replacement value. I need to replace the parenthesis to what it evaluates to. Is there a way to do this. Here is what I have so far.
public int solve(string etq)
{
Regex rgx = new Regex(@"\(([^()]|(?R))*\)");
MatchCollection matches;
matches = rgx.Matches(etq);
foreach(Match m in matches)
{
//replace m in etq with unique value here
}
//calculations here
return calculation
}
Regex.replace(...) replaces all occurrences of the pattern specified. I want to be able to match to multiple scenario and replace each scenario with different output