regex to validate double values

前端 未结 2 1163
自闭症患者
自闭症患者 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());
    }
    
    0 讨论(0)
  • 2021-01-27 04:20
    ^[0-9]{0,2}\.?[0-9]{0,2}$
    

    you can try it out here: http://www.regular-expressions.info/javascriptexample.html

    0 讨论(0)
提交回复
热议问题