Expression Expressions for 5-Digit Code Search

Trying to find pattern matches based on the following conditions:

  • String length - 5 characters
  • Char [0] = Letter / Number
  • Char [1] = Letter
  • Char [2-4] = Number

I do not understand why "22222" works for this expression?

 p = r'(\w|\d)(\w)(\d){3,}'
 m = re.match(p, "AA012")    # Works as expected
 --> 'AA012'

 m = re.match(p, "1A222")    # Works as expected
 --> '1A222'

 m = re.match(p, "22222")    # Does NOT work as expected!
 --> '22222'

What am I missing in the syntax of a regex expression?

0
source share
1 answer

\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}'
+2
source

All Articles