Check if string matches pattern
Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzle Game 5 Looping
--
Chapters
00:00 Question
00:34 Accepted answer (Score 655)
00:44 Answer 2 (Score 340)
01:06 Answer 3 (Score 55)
01:22 Answer 4 (Score 31)
01:35 Thank you
--
Full question
https://stackoverflow.com/questions/1259...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #regex #stringmatching
#avk47
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzle Game 5 Looping
--
Chapters
00:00 Question
00:34 Accepted answer (Score 655)
00:44 Answer 2 (Score 340)
01:06 Answer 3 (Score 55)
01:22 Answer 4 (Score 31)
01:35 Thank you
--
Full question
https://stackoverflow.com/questions/1259...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #regex #stringmatching
#avk47
ACCEPTED ANSWER
Score 712
import re
pattern = re.compile("^([A-Z][0-9]+)+$")
pattern.match(string)
ANSWER 2
Score 391
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 evaluate it as bool if needed
>>> bool(re.match(r"hello[0-9]+", 'hello1'))
True
ANSWER 3
Score 58
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())
ANSWER 4
Score 32
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'