You can save one character using
[0-9]{9}([0-9]{3})?
but, in my opinion, your path is better, because it more clearly conveys your intention. Regexes is hard to read already.
Of course, you can use \d instead of [0-9] .
(Edit: At first, I thought you could drop the parens around [0-9]{3} , but you cannot, the question mark will be ignored. This way you only save one character, not three.)
(Edit 2: you also need to bind the regex with ^ and $ (or \b ) or re.match() , will also match 123456789 inside 1234567890 )
source share