I would like to check for string “tDDDDD” where D has to be digits and should not be more than the length (minimum 4, maximum 5) of it.
No other characters allowed.
Currently my code checks like this,
m = re.match('^(t)(\d+)', changectx.branch())
But is also allows t12345anythingafterit.
I changed the regular expression to
'^(t)(\d\d\d\d)(\d)?$'
Is this correct or any smart way of doing it?
Your regular expression will work, but you could also use this regular expression:
The
{4,5}is a quantifier that means the previous token must occur between 4 and 5 times.The parentheses are only necessary here if you wish to capture the matching parts of the string.