正则表达式来验证双值 [英] regex to validate double values

查看:38
本文介绍了正则表达式来验证双值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试提出一个正则表达式来验证双精度值.我承认我在正则表达式方面很垃圾,真的应该买一本书......无论如何,范围很大,所以这里是:

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 到 99.99 是范围,前导 '00' 是可选的,'.' 也是可选的.尾随的.00"也是如此.所以用户可以输入 0.1 00.01, 0.11, 1, 1.0 1.00 这些都是有效的.

.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 would be valid.

谢谢,r.

推荐答案

与其使用 RegEx,为什么不使用 double 的 TryParse 方法?

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());
}

这篇关于正则表达式来验证双值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆