Rony Rony - 7 months ago 16
Python Question

Puzzled about use of regex to match exactly 3 letters followed by exactly 3 numbers

I'm a Python rookie and am trying to match words in sentences that are exactly 3 letters (small or capital) followed by exactly 3 numbers. Here's my code:

def regex():
pattern = r'^[a-zA-Z]{3}\d{3}$'
found = re.search(pattern, "My word is bla123")
print(found)


The problem is the ^. If I remove it, bla123 is matched, but so is blaa123. If I add the ^ to set a word bound, bla123 isn't matched. All my research here and elsewhere has yielded the same pattern, starting with ^. Some suggestions were to use \b as prefix and suffix, but that didn't work for me either.

Please help. I'm sure there's something I'm overlooking over and over again. Thanks!

Answer

You can remove the ^ and $ checks, add word boundaries (\b):

>>> pattern = r'\b[a-zA-Z]{3}\d{3}\b'
>>> re.findall(pattern, "My word is bla123")
['bla123']
>>> re.findall(pattern, "My word345 is bla123")
['bla123']
>>> re.findall(pattern, "My word345 and bla56 is bla123 and abc343")
['bla123', 'abc343']
Comments