Sunday, May 12, 2024
 Popular · Latest · Hot · Upcoming
32
rated 0 times [  38] [ 6]  / answers: 1 / hits: 195009  / 13 Years ago, wed, december 21, 2011, 12:00:00

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)


More From » regex

 Answers
49

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.


[#88439] Tuesday, December 20, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
emiliano

Total Points: 381
Total Questions: 109
Total Answers: 93

Location: Jersey
Member since Fri, Oct 1, 2021
3 Years ago
emiliano questions
;