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 1-31, 1-32, 0-99, 0-100, 1-100,1-127, 0-255, 0-999, 1-999, 1-1000 and 1-9999.
Thefirst important thing to keep in mind about regular expressions is thatthey don't know numbers, they don't know counting and they can notcomprehend 1-100 means any number from 1 to 100. The reason is regexdeals with text only and not numbers, hence you have to take a littlecare while dealing with numbers and number ranges or numeric ranges inregex match, search, validate or replace operations.
If you want to learn Regex for Numbers and any Number Range with logic and Simple & Practical Examples, I will suggest you to see this simple and to the point Regex Course with step by step approach. This video course teaches you the Logic and Philosophy of Regular Expressions for different number ranges.
Just for anexample, lets say if you want to match any number from 1 to 100 and youwrite regex for it as
/ [1-100]/
andhope that it will match all the numbers from 1 to 100, then your regexwill work but give unexpected results. This regex will match only twonumbers, yes, only two numbers and NO doubt about that. Can you figureout which two numbers? If you look at it you will come to know that itwill match 0 and 1 only and nothing else. Similarly the range[0-255] will match 0,1,2,5. First is the range 0-2 which is in acharacter 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 numberranges in Regular expressions.
Numbers in Regex
The simplestmatch 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 successfulmatch. Similarly to match 2019 write / 2019 / and it is a numberliteral match. But you can see its not flexible as it is very difficultto know about a particular number in text or the number may occur inranges.
\d forsingle or multiple digit numbers
To match anynumber from 0 to 9 we use \d in regex. It will match any single digitnumber from 0 to 9.
\dmeans [0-9] or match any number from 0 to 9.Instead ofwriting 0123456789 the shorthand version is [0-9] where [] is used forcharacter range.
[1-9][0-9]will match double digit number from 10 to 99.
But if youwant to match number of any number of digits like 2,55,235, 9875 aquantifier is added at the end
/ \d+ / where + is a quantifier which matches between oneand as many times as possible.
Two digit or three digitnumber match
Tomatch a two digit number / \d{2} / is used where {} is aquantifier and 2 means match two times or simply a two digit number.Similarly / \d{3} / is used to match a three digit number and soon.
Regex Match for Number Range
Nowabout 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 ofstring.
Regex for range 0-9
To matchnumeric range of 0-9 i.e any number from 0 to 9 the regex is simple
/[0-9]/
Regex for 1 to 9
To match anynumber from 1 to 9, regular expression is simple
/[1-9]/
Similarly youmay use /[3-7]/ to match any number from 3 to 7 or /[2-5]/ to match2,3,4,5
Regexfor0 to 10
Tomatch numbers from 0 to 10 is the start of a little complication, notthat much, but a different approach is used. This series is broken into two components.
1. from 0 to 9
2. 10
And the regexwill be written for the components
/\b([0-9]|10)\b /
Explanation:
For the twocomponents we are writing two pieces
1. from 0 to9 [0-9] &
2.10 10 and we are using a group and using|operator which is called OR operator which means either 0-9or 10here.
Regex for1 to10
Similarly for1 to 10 the regex will be
\b([1-9]|10)\b
Regexfor1 to 12
\b([1-9]|1[0-2])\b
The rangefrom 1 to 12 is divided in to two ranges
1. 1 to 9--> [1-9]
2. 10 to 12--> 1[0-2]
Regex for 1to 16
\b([1-9]|1[0-6])\b
In this casethe range is divided in to
1. 1 to 9
2. 10 to 16
Regexfornumber range 1-31
\b([1-9]|[12][0-9]|3[01])\b
Here the range from 1-31is divided in to three components per requirement
1. 1 to 9 --> [0-9]
2. 10 to 29 -->[12][0-9]
3. 30 to 31 -->3[01]
Regexfor1-32
The regex for1 to 32 is
\b([1-9]|[12][0-9]|3[0-2])\b
1. 1 to 9 --> [0-9]
2. 10 to 29 -->[12][0-9]
3. 30 to 32 -->3[02]
Regex for0-99
The regularexpression for range 0 to 99 is
\b([0-9]|[1-9][0-9])\b
This range isdivided in to two ranges
1. 0 to 9--> [0-9]
2. 10 to 99--> [1-9][0-9]
Regex for0-100
Regex forrange 0 to 100 is
\b([0-9]|[1-9][0-9]|100)\b
Here therange is divided in to three components, and the additional componentthen the previous range is number 100.
Regexfor1-100
Regularexpression for this range is
\b([1-9]|[1-9][0-9]|100)\b
1. Firstcomponent is 1 to 9
2. Secondpart is 10 to 99
3. Third part is 100
Regex for1-127
Regex forrange 1 to 127 is
\b([1-9]|[1-9][0-9]|1[01][0-9]|12[0-7])\b
The numericrange 1 to 127 is divided in to
1. 1 to 9
2. 10 to 99
3. 100 to 119
4. 120 to 127
Regexfor0-255
This range isalso divided in to three parts.
1. 0-199
The regex forthis component is
[01]?[0-9][0-9]?
2. The secondpart is 200-249 and regex for this part is
2[0-4][0-9]
3. Finallythe last part is 250-255
25[0-5]
The completeregex is
/\b([01]?[0-9][0-9]?|2[0-4][0-9]|25[0-5])
For moredetails please see regexfor ip address
Regexfor0-999
([0-9]|[1-9][0-9]|[1-9][0-9][0-9])
The regex forrange 0 to 999 is split in to three sections,
1. 0 to 9
2. 10 to 99
3. 100 to 999
Regexfor1-999
Regularexpression for 1-999 is
([1-9]|[1-9][0-9]|[1-9][0-9][0-9])
Regexfor number range 1-1000
Regex code to match range from 1to 1000 is
([1-9]|[1-9][0-9]|[1-9][0-9][0-9]|1000)
Regexfor1-9999
Regex forrange 1 to 9999 is
([1-9]|[1-9][0-9]|[1-9][0-9][0-9]|[1-9][0-9][0-9][0-9])