正则表达式允许从-100到+200的十进制数字 [英] Regex to allow decimal numers from -100 to +200

查看:315
本文介绍了正则表达式允许从-100到+200的十进制数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

谁能在正则表达式上帮助我,允许输入从-100.000到+200.000的数字.

在此先感谢

问候
Sherin

Hi All,

Can anyone help me on regular expression to allow numbers from -100.000 to +200.000.

Thanks in advance

Regards
Sherin

推荐答案

我同意Hiren:您可以使用Regex,但是用大锤敲碎螺母.
问题在于正则表达式必须处理:
200
199
7.888
-13.6
但拒绝
-100.001
首先不容易写,也不容易维护(如果限制发生变化会怎样?).

这不是一个很好的Regex任务:如果必须从字符串执行此操作,而不是通过这种方式执行操作:
I agree with Hiren: you could use a Regex, but it would be a sledgehammer to crack a nut.
The trouble is that the regex would have to cope with:
200
199
7.888
-13.6
but reject
-100.001
Not easy to write in the first place, not easy to maintain (what if the limits change?).

It is not a good Regex task: if you must do this from a string than do it this way:
bool ValidateFloat(string s)
    {
    float f;
    if (float.TryParse(s, out f))
        {
        if ((f >= -100.000) && (f <= 200.000))
            {
            return true;
            }
        }
    return false;
    }

如果没有别的,它更容易阅读和理解!

If nothing else, it is a lot easier to read and understand!


这篇关于正则表达式允许从-100到+200的十进制数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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