0

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... :)

JorgeLL
  • 3
  • 1
  • Ensure to 1) post actual code (some of the snippets above seem like they should perhaps contain *strings* ..) and 2) the minimal code to show the issue at hand. In any case, it may be worthwhile to separate the tokenizing ("regex" to token stream) from the parsing (this doesn't need to be complex), ref. https://stackoverflow.com/a/380487/2864740 – user2864740 Dec 21 '21 at 20:37
  • That said, I recommend a LL(1) recursive descent parser, when such fits, due to simplicity. This can also perform evaluation in the same step. – user2864740 Dec 21 '21 at 20:43
  • Hi @user2864740, thanks for the comments. The parser you recommend I didn't know of it (I am very new in the topic), I will look into it and maybe change the one I have. Regarding my question, to separate and tokenize, I do it after the change I want to make, maybe it is worth doing it before. Anyways, I came up with something that works: trim = Regex.Replace(trim, userDefinedFunctions[counterForFunctions].UserFunctionSymbol + @"(.*?)[)]", "("+auxString2+")"); It is ugly but seems to do the trick :) Still open to more ideas! – JorgeLL Dec 22 '21 at 08:31

0 Answers0