Check if string matches pattern
--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzle Meditation
--
Chapters
00:00 Check If String Matches Pattern
00:23 Accepted Answer Score 710
00:30 Answer 2 Score 391
00:47 Answer 3 Score 58
00:59 Answer 4 Score 32
01:08 Answer 5 Score 26
01:38 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
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzle Meditation
--
Chapters
00:00 Check If String Matches Pattern
00:23 Accepted Answer Score 710
00:30 Answer 2 Score 391
00:47 Answer 3 Score 58
00:59 Answer 4 Score 32
01:08 Answer 5 Score 26
01:38 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'