best way to do a series of regexp checks with groups

wittempj at hotmail.com wittempj at hotmail.com
Sun Jan 23 11:29:16 EST 2005


what about something like this?
>>> import re
>>> m = re.match(r"""(?P<operator>add|mult) (?P<int_1>\d+)
(?P<int_2>\d+)""", 'add 3 5')
>>> from operator import add, mul
>>> op = {'add': add, 'mult: mul}
>>> op[m.groupdict()['operator']](int(m.groupdict()['int_1']),
int(m.groupdict()['int_2']))
8




More information about the Python-list mailing list