class REMatcher(object):

    def __init__(self, matchstring):
        self.matchstring = matchstring

def match(self, regexp):
    self.rematch = re.match(regexp, self.matchstring)
return bool(self.rematch)

def group(self, i):
    return self.rematch.group(i)
for statement in ('I love Mary', 'Ich liebe Margot', "Je t'aime Marie",
    'Te amo Maria'):
    m = REMatcher(statement)
if m.match('I love (\\w+)'):
    print('He loves', m.group(1))
elif m.match('Ich liebe (\\w+)'):
    print('Er liebt', m.group(1))
elif m.match("Je t'aime (\\w+)"):
    print('Il aime', m.group(1))
else:
    print('???')
Match groups in Python