开发者

regex to validate double values

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:

.01 to 99.99, is the range, with the leading '00' being optional, as is the '.' and the same for the trailing '.00'. So the user could type in 0.1 00.01, 0.11, 1, 1.0 1.00 and these all woul开发者_运维问答d be valid.

Thanks, r.


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-9]{0,2}\.?[0-9]{0,2}$

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜