0

I need help and guidance for a mechanism of detecting, replacing with numbers and evaluating math formulas in a string. The fomula is enclosed in a pre-defined starr and end symbol.
Example:

A train starts at \[a+b*c\] m/s . it acceelates at \[a/b\] m/s2 

Now we have values of a & b , we want to replace the values in the string and evaluate expression.
Like so

if a=1, b=2,c=3

A train starts at 7 m/s . it acceelates at 0.5 m/s2 

I'm kindly asking for assist with a possible example in C# of how can this be achieved?

Any help is appreciated. (I'm almost alien to regex)

EDIT: Apologies I forgot to mention, the variables / their vals, their symbol are all unknown. We need to evaluate this dynamically. String are entered by user and below he/she tells the variables used and their value range.

Dave
  • 434
  • 5
  • 22
Abdul Ali
  • 1,905
  • 8
  • 28
  • 50
  • possible duplicate of [Is there a string math evaluator in .NET?](http://stackoverflow.com/questions/355062/is-there-a-string-math-evaluator-in-net) – steve cook Oct 02 '14 at 07:37
  • See the usage of NCalc http://stackoverflow.com/a/13256816/932418 – L.B Oct 02 '14 at 07:48
  • thank you. just one more help. as said I am not aware of Regex , can you assist how can the above expression be divided into string parts and formula parts, e.g. above example there are two formula parts, can you assist how to get those 2 parts as seperate string (list of strings) . and Thank you for the library references – Abdul Ali Oct 02 '14 at 07:57
  • Is there any identifier that from here the expression starts ? because look at this example `A train starts at \[at\] m/s . it acceelates at \[a/t\] m/s2`. and variables are `a` and `t` how will you differentiate `at(word)` and `\[at\](expression)` ? – NeverHopeless Oct 02 '14 at 07:57
  • one of the easiest method to evaluate math expression is by using RPN (Reverse Polish Notation) and stack, have a look at http://www.codeproject.com/Tips/381509/Math-Parser-NET-Csharp and http://www.codeproject.com/Articles/752516/Math-Equation-Parsing-using-Call-Stacks – user3473830 Oct 02 '14 at 07:58
  • @NeverHopeless the math expressions are inside `\[` and end at `\]` . in your example , we will use `a*t` not `at` for a product – Abdul Ali Oct 02 '14 at 07:59

2 Answers2

1

You can try this regex:

(?<=\[)(.+?)(?=\\\])

and pass this matched expression to any parser suggested in comments and other answers.

Demo

Community
  • 1
  • 1
NeverHopeless
  • 11,077
  • 4
  • 35
  • 56
0
String.Format("A train starts at {0} m/s . it acceelates at {1} m/s2", a+b*c, a\b);
mybirthname
  • 17,949
  • 3
  • 31
  • 55
  • That looks good, but it is clearly not what the OP wants at all. _Detect_ and _evaluate_ are the key words, not _placing_.. – TaW Oct 02 '14 at 07:39
  • @Taw yes because lets learn the guys to use a tank to kill a bird. Here we don't need a Regex ... – mybirthname Oct 02 '14 at 08:22
  • Indeed, not a RegEx, most likely. Rather a clarification about the rules the users should follow for entering formulas. Embedded in the text? Marked up how? In a separate field?.. We don't know. - The real question is about __evaluating__ and the real answer is in the likns above. – TaW Oct 02 '14 at 08:45