King-Leonidas/regexmultimatch.py

17 lines
356 B
Python

import re
def multimatch(table, line):
for regex, callback in table:
m = re.match(regex, line)
if m:
return callback(m)
if __name__ == "__main__":
def f1(m):
return "f1"
def f2(m):
return "f2"
tbl = [("foo", f1), ("bar", f2)]
print(multimatch(tbl, "foo"))
print(multimatch(tbl, "bar"))