regex to validate double values

前端 未结 2 1193
自闭症患者
自闭症患者 2021-01-27 03:55

I\'m trying to come up with a regex to validate a double value. I will admit that I am crap at regex and really should buy a book... Anyway the range is large so here goes:

2条回答
  •  醉酒成梦
    2021-01-27 04:08

    Rather than a RegEx, why not use double's TryParse method?

    string[] sa = new string[] { "00.01", "1.00", "xx" };
    double d;
    bool isValid;
    foreach (string s in sa)
    {
        isValid = double.TryParse(s, out d) && d >= 0.01d && d <= 99.99d;
        Console.WriteLine("{0}: {1}", s, isValid.ToString());
    }
    

提交回复
热议问题