from re import * ''' Fast Regular Expression Finder: Designed to handle two problems of regular expressions 1) Easy multiple extractions: (as seen in Perl) /(\w+) - (\d+) = (a*b+c?)/ ==> three variables matching the three clusters ($1, $2, and $3 in Perl). Hence, with freg, you can do: a, b, c = fmatch(some_string, r"(\w+) - (\d+) = (a*b+c?)") This requires you to know that the match will succeed. If it does not succeed, a NoMatch exception will be raised. 2) Automatic caching of compiled regexes. This was really easy to implement, though I haven't tested whether it actually improves performance. It can be turned off pretty easily with minor changes to the code. ''' regcache = {} def fmatch(string, regex): r = 0 if regcache.has_key(regex): r = regcache[regex] else: r = compile(regex) regcache[regex] = r m = r.match(string) if m: return m.groups() else: raise "NoMatch", '/%s/ on "%s"' % (regex, string) if __name__ == '__main__': a,b,c = fmatch('123 abcdef aabbbaac', r'(\d+) (\w+) ([ab]+?)') print "a: %s b: %s c: %s" % (a,b,c) a,b,c = fmatch('123 abcdefaabbbaac', r'(\d+) (\w+) ([ab]+?)')