Check if string matches pattern
721,282
Solution 1
import re
pattern = re.compile("^([A-Z][0-9]+)+$")
pattern.match(string)
Solution 2
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
Solution 3
Please try the following:
import re
name = ["A1B1", "djdd", "B2C4", "C2H2", "jdoi","1A4V"]
# Match names.
for element in name:
m = re.match("(^[A-Z]\d[A-Z]\d)", element)
if m:
print(m.groups())
Solution 4
import re
import sys
prog = re.compile('([A-Z]\d+)+')
while True:
line = sys.stdin.readline()
if not line: break
if prog.match(line):
print 'matched'
else:
print 'not matched'
Solution 5
import re
ab = re.compile("^([A-Z]{1}[0-9]{1})+$")
ab.match(string)
I believe that should work for an uppercase, number pattern.
Author by
DanielTA
Updated on September 22, 2021Comments
-
DanielTA over 2 years
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 ^
-
conradkleinespel over 7 yearsFrom the docs on
re.match
:If zero or more characters at the beginning of string match the regular expression pattern
. I just spent like 30 minutes trying to understand why I couldn't match something at the end of a string. Seems like it's not possible withmatch
, is it? For that,re.search(pattern, my_string)
works though. -
CrazyCasta over 7 years@conradk Yes, you're right, I think there's something like an implied
^
at the beginning when you usematch
. I think it's a bit more complicated then that very simple explanation, but I'm not clear. You are correct that it does start from the beginning of the string though. -
Rick Smith over 7 yearsThis is the only case that returns the match which is required for getting groups. Best answer in my opinion.
-
LondonRob about 6 yearsThat's weird. Why can you use
re.match
in the context of anif
, but you have to usebool
if you're using it elsewhere? -
LondonRob about 6 yearsCareful with
re.match
. It only matches at the start of a string. Have a look atre.search
instead. -
Dennis about 5 years@LondonRob probably because
if
checks for the match not beingNone
. -
Suh Fangmbeng about 4 yearsThere's a big need to compile to make sure there are no errors in the regular expressions like bad character range errors
-
nehem about 4 years@SuhFangmbeng Compilation is useful when the same
re
is used in more than one places to improve efficiency. In terms of error.match
would throw the same error what.compile
does. It's perfectly safe to use. -
Robo Robok about 3 yearsI edited your answer, because it only makes sense with
search()
in this context. -
miksus almost 3 years@nehem actually all of the regex functions in
re
module compile and cache the patterns. Therefore there is absolutely no efficiency gain using compile and then match than just directly callingre.match
. All of these functions call the internal function_compile
(includingre.compile
) which does the caching to a python dictionary. -
CrazyCasta over 2 yearsYes, but that's what the questioner wants. I'm not sure what you mean by "only makes sense with
search()
". It works perfectly fine with match. -
CK5 almost 2 yearsbest answer among other answers
-
Montana Burr almost 2 yearsTo be clear: You probably want to check if pattern.match returns something; luckily "None" is truthy, so you can just do "if pattern.match:"