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 ^
Method : Using join regex + loop + re.match() This task can be performed using combination of above functions. In this, we create a new regex string by joining all the regex list and then match the string against it to check for match using match() with any of the element of regex list.
Variants of matches() method is used to tell more precisely not test whether the given string matches to a regular expression or not as whenever this method is called in itself as matches() or be it matches() where here we do pass two arguments that are our string and regular expression, the working and output remains ...
fullmatch(). This method checks if the whole string matches the regular expression pattern or not. If it does then it returns 1, otherwise a 0.
import re pattern = re.compile("^([A-Z][0-9]+)+$") pattern.match(string)
One-liner: re.match(r"pattern", string) # No need to compile
import re >>> if re.match(r"hello[0-9]+", 'hello1'): ... print('Yes') ... Yes
You can evalute it as bool
if needed
>>> bool(re.match(r"hello[0-9]+", 'hello1')) True
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With