简体   繁体   English

RegEx:如何匹配所有大于 49 的数字?

[英]RegEx: How can I match all numbers greater than 49?

I'm somewhat new to regular expressions and am writing validation for a quantity field where regular expressions need to be used.我对正则表达式有些陌生,并且正在为需要使用正则表达式的数量字段编写验证。

How can I match all numbers greater than or equal to 50?如何匹配所有大于或等于 50 的数字?

I tried我试过了

[5-9][0-9]+

but that only matches 50-99.但这仅匹配 50-99。 Is there a simple way to match all possible numbers greater than 49?有没有一种简单的方法来匹配所有可能大于 49 的数字? (only integers are used) (仅使用整数)

The fact that the first digit has to be in the range 5-9 only applies in case of two digits.第一个数字必须在5-9范围内的事实仅适用于两位数的情况。 So, check for that in the case of 2 digits, and allow any more digits directly:因此,在 2 位的情况下进行检查,并直接允许更多的数字:

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

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.表示“或”,因此[5-9]\d或任何具有 3 位或更多位的数字。 \d is simply a shortcut for [0-9] . \d只是[0-9]的快捷方式。

Edit: To disallow numbers like 001 :编辑:禁止像001这样的数字:

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

This forces the first digit to be not a zero in the case of 3 or more digits.在 3 位或更多位的情况下,这会强制第一个数字不是零。

I know there is already a good answer posted, but it won't allow leading zeros.我知道已经发布了一个很好的答案,但它不允许前导零。 And I don't have enough reputation to leave a comment, so... Here's my solution allowing leading zeros:而且我没有足够的声誉来发表评论,所以......这是我允许前导零的解决方案:

First I match the numbers 50 through 99 (with possible leading zeros):首先,我匹配数字 50 到 99(可能有前导零):

0*[5-9]\d

Then match numbers of 100 and above (also with leading zeros):然后匹配 100 及以上的数字(也带有前导零):

0*[1-9]\d{2,}

Add them together with an "or" and wrap it up to match the whole sentence:将它们与“或”相加并将其包裹起来以匹配整个句子:

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

That's it!而已!

尝试匹配50-99或任何三个或更多数字的字符串的条件组:

var r = /^(?:[5-9]\d|\d{3,})$/

Next matches all greater or equal to 11100 :接下来匹配所有大于或等于11100

^([1-9][1-9][1-9]\d{2}\d*|[1-9][2-9]\d{3}\d*|[2-9]\d{4}\d*|\d{6}\d*)$

For greater or equal 50 :对于大于或等于50

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

See pattern and modify to any number.查看模式并修改为任何数字。 Also it would be great to find some recursive forward/backward operators for large numbers.此外,为大量数字找到一些递归前向/后向运算符也很棒。

I know this is old, but none of these expressions worked for me (maybe it's because I'm on PHP).我知道这是旧的,但这些表达式都不适合我(也许是因为我在 PHP 上)。 The following expression worked fine to validate that a number is higher than 49:以下表达式可以很好地验证数字是否高于 49:

/([5-9][0-9])|([1-9]\d{3}\d*)/

Here is a regex that matches numbers from 31 to 99999, which you can adjust to your needs:这是一个匹配从 31 到 99999 的数字的正则表达式,您可以根据需要进行调整:

^(?:[3][1-9]|[4-9][0-9]|(^[1-9][0-9]{2,4}$))$ where ^(?:[3][1-9]|[4-9][0-9]|(^[1-9][0-9]{2,4}$))$其中

  • [3][1-9] - matches numbers from 31 to 39 [3][1-9] - 匹配从 31 到 39 的数字
  • [4-9][0-9] - matches numbers from 40 to 99 [4-9][0-9] - 匹配从 40 到 99 的数字
  • ^[1-9][0-9]{2,4} - matches numbers from 100 to 9999 ^[1-9][0-9]{2,4} - 匹配从 100 到 9999 的数字

The last bit ^[1-9][0-9]{2,4} can be changed to ^[1-9][0-9]{2,} to match any number greater than 100最后一位^[1-9][0-9]{2,4}可以更改为^[1-9][0-9]{2,}以匹配任何大于 100 的数字

试试这个正则表达式:

[5-9]\d+|\d{3,}

In my case I needed the number to be greater than 59. And this worked for me.就我而言,我需要这个数字大于 59。这对我有用。

[6-9][0-9]+|[1-9]\d{2,}

EDIT编辑

This also works.这也有效。

[6-9][0-9]|[1-9]\d{2,}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 正则表达式:匹配大于或等于 1001 的数字 - RegEx: Match numbers greater or equal than 1001 正则表达式-如何匹配小于或等于二十四的所有数字? - Regular Expression - How can I match all numbers less than or equal to Twenty Four? Javascript - 如何在正则表达式中搜索大于 x 的数字 - Javascript - How to search for numbers greater than x in regex 如何遍历对象以删除所有大于100的数字? - How would I loop through an object to remove all numbers greater than 100? 正则表达式匹配大于0的数字,括号内并且倒数第二个字符为字符串 - Regex to match numbers greater than 0, within parenthesis and if the second last character as string RegEx在Javascript中替换大于变量的数字 - RegEx replacing numbers greater than variable in Javascript 如何在“/”之前和之后生成最多 3 个数字字符的正则表达式,其中“/”之前的数字应该大于“/”之后的数字 - How to generate a regex with Max of 3 numeric chars before & after the "/" where Numbers before the "/" should be greater than numbers after the "/" 如何在 Javascript 中对大于 2^32 的数字进行按位和运算? - How do I do a bitwise and on numbers greater than 2^32 in Javascript? 我如何构建一个正则表达式来排除除字母和数字之外的所有内容? - How can I build a regex to exclude all, except letters and numbers? 如何在正则表达式中匹配所有 MyCode,包括嵌套标签? - How can I match all MyCode, including nested tags, in regex?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM