|
Home Contact us |
---|
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.
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 1-100 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 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 an example, lets say if you want to match any number from 1 to 100 and you write regex for it as
/ [1-100] /
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 [0-255] will match 0,1,2,5. First is the range 0-2 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 [0-9] or match any number from 0 to 9. Instead of writing 0123456789 the shorthand version is [0-9] where [] is used for character range.
[1-9][0-9] 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 0-9 i.e any number from 0 to 9 the regex is simple
/[0-9]/
To match any number from 1 to 9, regular expression is simple
/[1-9]/
Similarly you may use /[3-7]/ to match any number from 3 to 7 or /[2-5]/ 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([0-9]|10)\b /Explanation:
For the two components we are writing two pieces
1. from 0 to 9 [0-9] &
2. 10 10 and we are using a group and using | operator which is called OR operator which means either 0-9 or 10 here.
Similarly for 1 to 10 the regex will be
\b([1-9]|10)\b
\b([1-9]|1[0-2])\b
The range from 1 to 12 is divided in to two ranges
1. 1 to 9 --> [1-9]
2. 10 to 12 --> 1[0-2]
\b([1-9]|1[0-6])\b
In this case the range is divided in to
1. 1 to 9
2. 10 to 16
\b([1-9]|[12][0-9]|3[01])\b
Here the range from 1-31 is 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]
The regex for 1 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]
The regular expression for range 0 to 99 is
\b([0-9]|[1-9][0-9])\b
This range is divided in to two ranges
1. 0 to 9 --> [0-9]
2. 10 to 99 --> [1-9][0-9]
Regex for range 0 to 100 is
\b([0-9]|[1-9][0-9]|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([1-9]|[1-9][0-9]|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([1-9]|[1-9][0-9]|1[01][0-9]|12[0-7])\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. 0-199
The regex for this component is
[01]?[0-9][0-9]?
2. The second part is 200-249 and regex for this part is
2[0-4][0-9]
3. Finally the last part is 250-255
25[0-5]
The complete regex is
/\b([01]?[0-9][0-9]?|2[0-4][0-9]|25[0-5])For more details please see regex for ip address
([0-9]|[1-9][0-9]|[1-9][0-9][0-9])
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 1-999 is
([1-9]|[1-9][0-9]|[1-9][0-9][0-9])
Regex code to match range from 1 to 1000 is
([1-9]|[1-9][0-9]|[1-9][0-9][0-9]|1000)
Regex for range 1 to 9999 is
([1-9]|[1-9][0-9]|[1-9][0-9][0-9]|[1-9][0-9][0-9][0-9])