\w matches letters and numbers (as well as underscores).
Use [a-zA-Z]if you want to combine only letters:
r'\w[a-zA-Z]\d{3,}'
which corresponds to a letter or number (or underscore), then a letter, and then 3 numbers.
Demo:
>>> import re
>>> p = r'\w[a-zA-Z]\d{3,}'
>>> re.match(p, "22222")
>>> re.match(p, "AA012")
<_sre.SRE_Match object at 0x105aca718>
>>> re.match(p, "1A222")
<_sre.SRE_Match object at 0x105aca780>
>>> re.match(p, "_A222")
<_sre.SRE_Match object at 0x105aca718>
If underlining is a problem, use:
r'[a-zA-Z\d][a-zA-Z]\d{3}'
source
share