Regular expression match objects - compact syntax?

Diez B. Roggisch deetsNOSPAM at web.de
Fri Feb 4 05:47:21 EST 2005


Johann C. Rocholl wrote:

> 
> How do you people handle this?

Usually we don't bothe too much. But it has been suggested to do something
like this:

class Matcher:
    def __init__(self, rex):
       self.rex = rex

    def match(self, s):
       self.m = self.rex.match(s)
       return not self.m is None

Then you can do

m = Matcher(rex)

if m.match(line):
    print m.m


Of course you can enhance the class mather so that all methods it does not
define by itself are delegated to self.m, which would be more convient. But
I'm currently too lazy to write that down :)
-- 
Regards,

Diez B. Roggisch



More information about the Python-list mailing list