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

JavascriptRegex

Javascript Problem Overview


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)

Javascript Solutions


Solution 1 - Javascript

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.

Solution 2 - Javascript

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!

Solution 3 - Javascript

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

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

Solution 4 - Javascript

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.

Solution 5 - Javascript

Try this regex:

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

Solution 6 - Javascript

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*)/

Solution 7 - Javascript

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMaxxView Question on Stackoverflow
Solution 1 - JavascriptpimvdbView Answer on Stackoverflow
Solution 2 - JavascriptTiagoView Answer on Stackoverflow
Solution 3 - JavascriptmaericsView Answer on Stackoverflow
Solution 4 - JavascriptDzmitry LahodaView Answer on Stackoverflow
Solution 5 - JavascriptKirill PolishchukView Answer on Stackoverflow
Solution 6 - JavascriptPauGNUView Answer on Stackoverflow
Solution 7 - JavascriptDianaView Answer on Stackoverflow