普通防爆pression任何大于0的数字? [英] Regular Expression for any number greater than 0?

查看:115
本文介绍了普通防爆pression任何大于0的数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

应用验证,在MVC模型,并希望它使用正则表达式的事情。

Applying validation to a model in MVC and would like to do it using Regex.

展望验证我的模型中的ID是大于0的提交。

Looking to validate that an ID on my model is greater than 0 on submit.

我不熟悉正则表达式...可能有人帮助我吗?

I'm unfamiliar with Regex... could someone help me out?

感谢

推荐答案

我不知道如何MVC是相关的,但如果你的ID是一个整数,这BRE应该做的:

I don't know how MVC is relevant, but if your ID is an integer, this BRE should do:

    ^[1-9][0-9]*$

如果你想匹配的实数(浮点),而不是整数,你需要处理上面的情况下,在您的模式是0和1之间(即 0.25 ),以及您的模式有一个小数部分是0的情况下(即 2.0 )。虽然我们在这,我们将添加对整数前导零(即 005 )的支持:

If you want to match real numbers (floats) rather than integers, you need to handle the case above, along with cases where your pattern is between 0 and 1 (i.e. 0.25), as well as case where your pattern has a decimal part that is 0. (i.e. 2.0). And while we're at it, we'll add support for leading zeros on integers (i.e. 005):

    ^0*[1-9][0-9]*(\.[0-9]+)?|0+\.[0-9]*[1-9][0-9]*$

请注意,这第二个是一个扩展RE。同样的事情可以在基本RE pssed前$ P $,但几乎所有的东西理解ERE这些天。让我们打破了前pression分解成更易于消化的部分。

Note that this second one is an Extended RE. The same thing can be expressed in Basic RE, but almost everything understands ERE these days. Let's break the expression down into parts that are easier to digest.

    ^0*[1-9][0-9]*(\.[0-9]+)?

此匹配任何整数的的上述1.所以,我们的 2.0 将匹配,但 0.25的浮点数不会。在 0 * 在开始处理前导零,所以 005 == 5

This matches any integer or any floating point number above 1. So our 2.0 would be matched, but 0.25 would not. The 0* at the start handles leading zeros, so 005 == 5.

                             |

管道字符为或酒吧的在这种情况下。对于这个前pression评价的目的,它具有较高的precedence高于一切,除了 ^ $ ,所以它有效地用于连接两个常规的前pressions。

The pipe character is an "or-bar" in this context. For purposes of evaluation of this expression, It has higher precedence than everything except ^ and $, so it is effectively used to join two regular expressions.

和第二部分:

                              0+\.[0-9]*[1-9][0-9]*$

这与任意数量的与一个或多个 0 字符(更换 + * 来匹配零个或多个零,即 0.25 ),其次是一个时期,然后是一串数字,其中包括至少一个这不是一个 0 。所以这上面的匹配 0 中的一切内容 1

This matches any number that starts with one or more 0 characters (replace + with * to match zero or more zeros, i.e. .25), followed by a period, followed by a string of digits that includes at least one that is not a 0. So this matches everything above 0 and below 1.

当然,如果你让你的编程语言评估数字的东西,而不是试图匹配一个正规的前pression,你可以节省头痛的的CPU。

Of course, if you let your programming language evaluate something numerically rather than try to match it against a regular expression, you'll save headaches and CPU.

这篇关于普通防爆pression任何大于0的数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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