For a modern solution, head to my fork of regex-engine, range-ex
pip install range-ex
Pass a minimum and maximum value to the range_regex function to generate a regex that matches numbers in that range. The range is inclusive, meaning both the minimum and maximum values are included in the regex.
from range_ex import range_regex
regex1 = range_regex(5,89)
# ([5-9]|[2-7][0-9]|1[0-9]|8[0-9])
regex2 = range_regex(-65,12)
# (-[1-9]|-[2-5][0-9]|-1[0-9]|-6[0-5]|[0-9]|1[0-2])
Note: This will still find matches in strings like 1234 or abc25def53, so you may want to wrap it in ^ and $ to match the whole string or \b...\b to ensure word boundaries are matched.
If you only pass one of the two arguments, the other will be set to None, which means it will not be constrained.
In this case, the regex will match any number that is greater than or equal to the minimum or less than or equal to the maximum.
regex3 = range_regex(minimum=5)
# (([5-9])|[1-9]\\d{1}\\d*)
regex4 = range_regex(maximum=89)
# (-[1-9]\\d*|([0-9]|[2-7][0-9]|1[0-9]|8[0-9]))
This package resolves a bug in regex-engine for negative ranges including 0 and adds the option for unbounded ranges (lower/upper bound only).
"[0-9]+-[0-9]+".|operator has least priority, so even e.g.[1-9][0-9]is an accepted pattern: you should parenthesize the two parts before and after the hyphen. Also, a more compact formulation should exist."[123]\d{0,3}"(or"[0-3]\d{0,3}"to include 0's)?