Fix Python – Check if string matches pattern

Question

Asked By – DanielTA

How do I check if a string matches this pattern?

Uppercase letter, number(s), uppercase letter, number(s)…

Example, These would match:

A1B2
B10L1
C1N200J1

These wouldn’t (‘^’ points to problem)

a1B2
^
A10B
   ^
AB400
^

Now we will see solution for issue: Check if string matches pattern


Answer

import re
pattern = re.compile("^([A-Z][0-9]+)+$")
pattern.match(string)

This question is answered By – CrazyCasta

This answer is collected from stackoverflow and reviewed by FixPython community admins, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0