0

i want to get math equation only with addition such as 1+2+3 and return its result. i have the following code, and the problem is that it doesn't deal with doubles (i cant write 2.2+3.4) I tried to change the regex expression to ([\+-]?\d+.\d+)([\+-])(-?(\d+.\d+)) and now it doesnt deal with integers (i cant write 2+4). what should be the correct regex expression to deal with doubles and integers? thanx the code:

        regEx = new Regex(@"([\+-]?\d+)([\+-])(-?(\d+))");
        m = regEx.Match(Expression, 0);
        while (m.Success)
        {
            double result;
            switch (m.Groups[2].Value)
            {
                case "+":

                    result = Convert.ToDouble(m.Groups[1].Value) + Convert.ToDouble(m.Groups[3].Value);

                    if ((result < 0) || (m.Index == 0)) Expression = regEx.Replace(Expression, DoubleToString(result), 1);
                    else Expression = regEx.Replace(Expression, "+" + result, 1);
                    m = regEx.Match(Expression);
                    continue;
                case "-":
                    result = Convert.ToDouble(m.Groups[1].Value) - Convert.ToDouble(m.Groups[3].Value);
                    if ((result < 0) || (m.Index == 0)) Expression = regEx.Replace(Expression, DoubleToString(result), 1);
                    else Expression = regEx.Replace(Expression, "+" + result, 1);
                    m = regEx.Match(Expression);
                    continue;
            }
        }
        if (Expression.StartsWith("--")) Expression = Expression.Substring(2);
        return Expression;
    }
3
  • 3
    Would it not be easier to use something like String.Split()? Do you have to stick with regex?
    – Egor
    Aug 14, 2012 at 16:20
  • 2
    applying regex solution for every problem is a very BAD idea....as Egor suggested,y not use Split function!
    – Anirudha
    Aug 14, 2012 at 16:25
  • 4
    If you want to trying writing something that can parse and evaluate many mathematical expressions, by all means write a parser instead.
    – nhahtdh
    Aug 14, 2012 at 16:26

1 Answer 1

0

As the comments have stated, RegEx is not a good solution to this problem. You would be much better off with either a simple split statement (if you only want to support the + and - operators), or an actual parser (if you want to support actual mathematical expressions).

But, for the sake of explaining some RegEx, your problem is that \d+.\d+ matches "one or more digits, followed by any character, followed by one or more digits." If you gave it an integer greater than 99, it would work, since you're matching . (any character) and not \. (specifically the dot character).

A simpler version would be [\d\.]+, which matches one-or-more digits-or-dots. The problems is that it allows multiple dots, so 8.8.8.8 is a valid match. So what you really want is \d+\.?\d*, which matches one-or-more digits, one-or-zero dots, and zero-or-more digits. Thus 2, 2., and 2.05 are all valid matches.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.