I am new to c# and Regex and I am struggling with it. So far I figured out what I needed but I am very stuck with one case. I have tried many options from guides and forums but I do not see why it does not work and none of my alternatives are correct: I define two functions as follows:
_f(x)=_x+1
_g(x)=1/_x
Each of them are stored in a list, for example:
userDefinedFunctions[0].UserFunctionSymbol=_f
userDefinedFunctions[0].UserFunctionVariables[0]=_x
userDefinedFunctions[0].UserFunctionEquation=_x+1
So when I write to my "home-made" parser:
1+_f(1)+_g(1)
I store it as follows:
string trim =1+_f(1)+_g(1)
I would like to obtain: 1+(1+1)+(1/1) But I get: 1+(1+1) + (1+1) So, I manage to get the equations, substitute and store in variables:
string auxString1= _f(1)
string auxString2= (1)+1
But, when I try to use the auxString1 variable as pattern for:
trim = Regex.Replace(trim, auxString1, auxString2);
Something seems to be wrong because the replacements does not happen. After several alternatives I got this:
trim = Regex.Replace(trim, @"(?<=_)[a-z](.*?)(?=\))", "(" + auxString2);
trim = Regex.Replace(trim, @"\b[_](?=\()", "");
The results is: 1+((1)+1)+((1)+1). So I do not know how to use auxString1 as the pattern or how to replace only _f(1) and not all functions defined at the same time...
In case it is important, everything is inside a loop as follows to go through the input and replace what is needed:
for (int i = 0; i < userDefinedFunctions.Count; i++)
{
Regex r = new Regex(@"(?<=_)[a-z](.*?)(?=\))");
Match m = r.Match(trim);
while (m.Success)
{...
m = m.NextMatch();}//while
}//for
Thanks a lot for the help and the time!! If it is not doable, I will rewrite everything and think of a different way... :)