I want matched only the strings that represent numbers between 0...9999
import re
NUMERIC = re.compile("\d{,4}")
NUMERIC.match("324234")
nr =NUMERIC.match("324234")
nr.group(0)
Tried the above but it matches the first 4 digits from the string, even if the string has 5 digits.
Regex to match the numbers that have between 1 and 4 digits from this string represention of an integer number?
Anchors do the trick of not matching too much:
Note the
{1,4}— I’m assuming you don’t want to match the empty string. However, this will not match00001, which certainly is in range.A more robust alternative to regular expressions is to leverage Python’s built-in integer parsing: