RegEx:如何匹配所有大于49的数字? [英] RegEx: How can I match all numbers greater than 49?

查看:122
本文介绍了RegEx:如何匹配所有大于49的数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对正则表达式有点新,我正在为正常表达式需要的数量字段编写验证。

I'm somewhat new to regular expressions and am writing validation for a quantity field where regular expressions need to be used.

如何我可以匹配所有大于或等于50的数字吗?

How can I match all numbers greater than or equal to 50?

我试过


[5-9][0-9]+

但只匹配50-99。有没有一种简单的方法来匹配所有可能超过49的数字? (仅使用整数)

but that only matches 50-99. Is there a simple way to match all possible numbers greater than 49? (only integers are used)

推荐答案

第一个数字必须在 5范围内的事实-9 仅适用于两位数的情况。因此,在2位数的情况下检查,并允许更多数字:

The fact that the first digit has to be in the range 5-9 only applies in case of two digits. So, check for that in the case of 2 digits, and allow any more digits directly:

^([5-9]\d|\d{3,})$

此正则表达式具有开始/结束锚点确保你正在检查所有数字,字符串实际上代表一个数字。 | 表示或,因此 [5-9] \d 或任何包含3个或更多的数字数字。 \d 只是 [0-9] 的快捷方式。

This regexp has beginning/ending anchors to make sure you're checking all digits, and the string actually represents a number. The | means "or", so either [5-9]\d or any number with 3 or more digits. \d is simply a shortcut for [0-9].

修改:禁止 001 等数字:

^([5-9]\d|[1-9]\d{2,})$

在3位或更多位数的情况下,这会强制第一位数不为零。

This forces the first digit to be not a zero in the case of 3 or more digits.

这篇关于RegEx:如何匹配所有大于49的数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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