85

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?

I tried

[5-9][0-9]+

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

Alan Moore
  • 73,866
  • 12
  • 100
  • 156
Maxx
  • 3,925
  • 8
  • 33
  • 38
  • 1
    Is there some reason you don't use a regex to extract the numeric portion, then from there convert it and test it as a number? Seems like it would be much simpler. –  Dec 21 '11 at 16:02
  • It needs to use regex in order to be compliant with our system. It's too long and convoluted of a story for this comment block. Needless to say if there was another way, I would use it. @ridgerunner only integers are used. – Maxx Dec 27 '11 at 18:20

8 Answers8

122

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,})$

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].

Edit: To disallow numbers like 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.

pimvdb
  • 151,816
  • 78
  • 307
  • 352
  • 1
    Thank you for the explanation as well. I feel like I really learned something. +1 – Maxx Dec 21 '11 at 16:00
  • 2
    @pimvdb: Your use of `^` and `$` would imply that the entire string is numeric. This begs the question, why is a regex needed? –  Dec 21 '11 at 16:06
  • One minor issue is that it still matches '001' and other numbers with two leading zeroes, but I think this is probably ok. – Maxx Dec 21 '11 at 16:09
  • @ЖΞЖ: True, but it is explicitly stated in the question so there probably is some reason or another. @ Maxx: You'd have to disallow `0`s at the beginning. Please see my edit. – pimvdb Dec 21 '11 at 16:11
  • @pimvdb Fair enough, and I didn't want it to sound like I was attacking your answer. I'm just not convinced that the constraint placed on the solution is valid, especially since no sample input was provided. –  Dec 21 '11 at 16:15
  • Oh, awesome! Thanks! @ЖΞЖΞЖΞЖΞЖΞЖΞЖΞЖΞЖΞЖΞЖΞЖΞЖΞЖΞЖ - Yea, due to the nature of our system, regex has to be used. – Maxx Dec 21 '11 at 16:23
  • For MySQL: `^([5-9][0-9]|[1-9][0-9]{2,})$` – Can Aydoğan Apr 01 '13 at 09:53
  • 1
    inspiring, but number like 055 will not match, which is wrong. @Tiago's answer is the correct one. – Wang Dec 25 '14 at 08:24
  • I had to reverse the test so it matches unlimited first or else the counting-up-from pattern always caught to first 2 digits https://regex101.com/r/0eH75W/2 – Jack Jan 23 '23 at 16:29
30

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):

0*[5-9]\d

Then match numbers of 100 and above (also with leading zeros):

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!

Tiago
  • 432
  • 4
  • 8
4

Try a conditional group matching 50-99 or any string of three or more digits:

var r = /^(?:[5-9]\d|\d{3,})$/
maerics
  • 151,642
  • 46
  • 269
  • 291
3

Next matches all greater or equal to 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:

^([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.

Dzmitry Lahoda
  • 939
  • 1
  • 13
  • 34
2

In my case I needed the number to be greater than 59. And this worked for me.

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

EDIT

This also works.

[6-9][0-9]|[1-9]\d{2,}
Muhammad Russell
  • 361
  • 4
  • 13
2

Try this regex:

[5-9]\d+|\d{3,}
Kirill Polishchuk
  • 54,804
  • 11
  • 122
  • 125
1

I know this is old, but none of these expressions worked for me (maybe it's because I'm on PHP). The following expression worked fine to validate that a number is higher than 49:

/([5-9][0-9])|([1-9]\d{3}\d*)/
PauGNU
  • 783
  • 3
  • 12
  • 32
1

Here is a regex that matches numbers from 31 to 99999, which you can adjust to your needs:

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

  • [3][1-9] - matches numbers from 31 to 39
  • [4-9][0-9] - matches numbers from 40 to 99
  • ^[1-9][0-9]{2,4} - matches numbers from 100 to 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

Diana
  • 935
  • 10
  • 31
  • Doesn't work when leading zeros are present – Wili Whitelaw Mar 23 '23 at 09:41
  • It would be also a complete different requirement than 31 to 99999. My example was not meant to do it. As a solution: you can add a group at the beginning of this regex that would allow any number of 0s in front. Other solution (if zeros are not important - to remove zeros before parsing). – Diana Mar 23 '23 at 09:50