import re
def scan_compile(pattern):
pattern = pattern.replace('{str}', '(.+?)')
return re.compile(pattern.replace('{int}', '(\d+)'))
def scan_match(r, s):
match = r.match(s)
return [int(g) if g.isdigit() else g
for g in match.groups()]
>>> from scan_match import *
>>> r = scan_compile("{str} {int} {int}/{int}")
>>> scan_match(r, 'Mary Rose Jesus 12 24/32')
['Mary Rose Jesus', 12, 24, 32]