import re
re.compile(([0-9]|[A-Z0-9]))
Is this the correct way about doing it?
Thank you!
You need to provide re.compile()
a string, and your current regular expression will only match a single character, try changing it to the following:
import re
pattern = re.compile(r'^[A-Z\d]+$')
Now you can test strings to see if the match this pattern by using pattern.match(some_string)
.
Note that I used a raw string literal, which ensures the proper handling of backslashes.
The ^
at the beginning and $
at the end are called anchors, ^
matches only at the beginning of the string and $
matches only at the end of the string, they are necessary since you specified you want to only match strings that are entirely uppercase characters or digits, otherwise you could just match a substring.
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