
Home 

In this article you will learn how to match numbers and number range in Regular expressions. The Regex number range include matching 0 to 9, 1 to 9, 0 to 10, 1 to 10, 1 to 12, 1 to 16 and 131, 132, 099, 0100, 1100,1127, 0255, 0999, 1999, 11000 and 19999.
The first important thing to keep in mind about regular expressions is that they don't know numbers, they don't know counting and they can not comprehend 1100 means any number from 1 to 100. The reason is regex deals with text only and not numbers, hence you have to take a little care while dealing with numbers and number ranges or numeric ranges in regex match, search, validate or replace operations.
If you want to learn Regex Fast with plenty of Examples and Exercises, I will suggest you to see this simple and to the point Regex Course with practical examples and step by step approach with exercises. This video course teaches you the Logic and Philosophy of Regular Expressions from the Beginners level to advanced level.
Just for an example, lets say if you want to match any number from 1 to 100 and you write regex for it as
/ [1100] /
and hope that it will match all the numbers from 1 to 100, then your regex will work but give unexpected results. This regex will match only two numbers, yes, only two numbers and NO doubt about that. Can you figure out which two numbers? If you look at it you will come to know that it will match 0 and 1 only and nothing else. Similarly the range [0255] will match 0,1,2,5. First is the range 02 which is in a character class will match 0,1,2 and 5 written two times, will match 5. Now lets begin the logic and philosophy of matching numbers and number ranges in Regular expressions.
The simplest match for numbers is literal match. If you want to match 3 simply write / 3 / or if you want to match 99 write / 99 / and it will be a successful match. Similarly to match 2019 write / 2019 / and it is a number literal match. But you can see its not flexible as it is very difficult to know about a particular number in text or the number may occur in ranges.
To match any number from 0 to 9 we use \d in regex. It will match any single digit number from 0 to 9.
\d means [09] or match any number from 0 to 9. Instead of writing 0123456789 the shorthand version is [09] where [] is used for character range.
[19][09] will match double digit number from 10 to 99.
But if you want to match number of any number of digits like 2,55,235, 9875 a quantifier is added at the end
/ \d+ / where + is a quantifier which matches between one and as many times as possible.
To match a two digit number / d{2} / is used where {} is a quantifier and 2 means match two times or simply a two digit number. Similarly / d{3} / is used to match a three digit number and so on.
Now about numeric ranges and their regular expressions code with meaning. Usually a word boundary is used before and after number \b or ^ $ characters are used for start or end of string.
To match numeric range of 09 i.e any number from 0 to 9 the regex is simple
/[09]/
To match any number from 1 to 9, regular expression is simple
/[19]/
Similarly you may use /[37]/ to match any number from 3 to 7 or /[25]/ to match 2,3,4,5
To match numbers from 0 to 10 is the start of a little complication, not that much, but a different approach is used. This series is broken in to two components.
1. from 0 to 9
2. 10
And the regex will be written for the components
/\b([09]10)\b /Explanation:
For the two components we are writing two pieces
1. from 0 to 9 [09] &
2. 10 10 and we are using a group and using  operator which is called OR operator which means either 09 or 10 here.
Similarly for 1 to 10 the regex will be
\b([19]10)\b
\b([19]1[02])\b
The range from 1 to 12 is divided in to two ranges
1. 1 to 9 > [19]
2. 10 to 12 > 1[02]
\b([19]1[06])\b
In this case the range is divided in to
1. 1 to 9
2. 10 to 16
\b([19][12][09]3[01])\b
Here the range from 131 is divided in to three components per requirement
1. 1 to 9 > [09]
2. 10 to 29 > [12][09]
3. 30 to 31 > 3[01]
The regex for 1 to 32 is
\b([19][12][09]3[02])\b
1. 1 to 9 > [09]
2. 10 to 29 > [12][09]
3. 30 to 32 > 3[02]
The regular expression for range 0 to 99 is
\b([09][19][09])\b
This range is divided in to two ranges
1. 0 to 9 > [09]
2. 10 to 99 > [19][09]
Regex for range 0 to 100 is
\b([09][19][09]100)\b
Here the range is divided in to three components, and the additional component then the previous range is number 100.
Regular expression for this range is
\b([19][19][09]100)\b
1. First component is 1 to 9
2. Second part is 10 to 99
3. Third part is 100Regex for range 1 to 127 is
\b([19][19][09]1[01][09]12[07])\bThe numeric range 1 to 127 is divided in to
1. 1 to 9
2. 10 to 99
3. 100 to 119
4. 120 to 127
This range is also divided in to three parts.
1. 0199
The regex for this component is
[01]?[09][09]?
2. The second part is 200249 and regex for this part is
2[04][09]
3. Finally the last part is 250255
25[05]
The complete regex is
/\b([01]?[09][09]?2[04][09]25[05])For more details please see regex for ip address
([09][19][09][19][09][09])
The regex for range 0 to 999 is split in to three sections,
1. 0 to 9
2. 10 to 99
3. 100 to 999
Regular expression for 1999 is
([19][19][09][19][09][09])
Regex code to match range from 1 to 1000 is
([19][19][09][19][09][09]1000)
Regex for range 1 to 9999 is
([19][19][09][19][09][09][19][09][09][09])