Regex Numeric Range Generator


Numeric Ranges in Regular Expressions

Regular expressions are patterns used to match character combinations in strings. The challenge is that regex doesn't know what a number is. In other words, a regex doesn't know that 1-10 means any number from 1 to 10. Instead regex goes through the string character by character and checks if it matches the pattern.

So why not just convert a string to int before comparison? In some cases you need to find numbers inside strings and regular expression is often an option that performs well. There might be also products such as Google Analytics that format custom data values as strings. You need to use regex in Google Analytics for example in order to filter numeric values in custom dimensions.

With regex you have a couple of options to match a digit. You can use a number from 0 to 9 to match a single choice. Or you can match a range of digits with a character group e.g. [4-9]. If the character group allows any digit (i.e. [0-9]), it can be replaced with a shorthand (\d).

In order to match a multi-digit numeric ranges in regex, you need to approach the problem by splitting it into smaller chunks. The easies way to do this is to start by splitting the range based on how many digits each number has.

E.g. for range 3-13 you can have two groups a) 3-9 and b) 10-13. Group a) has a single digit and can be matched simply with regex '[3-9]', while group b) can has two digits. The first digit is 1 and the second digit 0-3. So the regex for group b) would be '1[0-3]'. The final regex simply combines these two with the alternation operator (|) resulting in '(3-9|1[0-3])' (i.e. 3-9 OR 1[0-3])

Example: Regex Number Range 1-20

Range 1-20 has both single digit numbers (1-9) and two digit numbers (10-20). For double digit numbers we have to split the group in two 10-19 (or in regex: "1[0-9]") and 20. Then we can join all these with an alternation operator to get "([1-9]|1[0-9]|20)".

Example: Regex Number Range 1-100

Range 1-100 has one, two and three digit numbers. So it's similar to 1-20, but now our two digit numbers are 10-99 (or in regex: "[1-9][0-9]"). And there's only one three digit number "100". Again we can join these with the alternation operator to get "([1-9]|[1-9][0-9]|100)".

Example: Regex Number Range 0-255

Again we have one, two and three digit numbers. Now our single digit numbers are 0-9, two digit numbers are 10-99 ("[1-9][0-9]"), but three digit numbers are 100-255. The three digit numbers need to be split futher into 100-199 ("1[0-9][0-9]" or in short "1[0-9]{2}") and 200-255, and the latter further into 200-249 ("2[0-4][0-9]") and 250-255 ("25[0-5]"). Combining all these with the alternation operator results in: "([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])".

Resources

to-regex-range Node.js package

This Regex Numeric Range Generator is built with JavaScript using an open source to-regex-range package. It is a function that takes two integers: the min value and max value (formatted as strings or numbers) and returns a valid regular expression.

Numeric Ranges in Regular-Expressions.info

A great resource of information about regular expression number ranges. It explains them in an easy-to-read format with good examples.

Regex for Numbers and Number Range at RegexTutorial.org

A tutorial that teaches you how to match numbers and number range in Regular expressions.