Is Perl *that* good?

Peter Hansen peter at engcorp.com
Tue Apr 27 14:48:28 EDT 2004


Paramjit Oberoi wrote:

>>    Asun> Regex is much more 'immediate' in Perl.  
>>
>>Sure, it's syntactically bound into the language.  There will always be an
>>extra constant overhead to enable regular expressions in Python.  That
> 
> 
> If only compiled regular expression objects allowed easy access to the
> last match object, python regxes would become significantly more
> convenient.  For example, today you have to write:
> 
> m = myregex.match(line)
> if (m):
>     xyz = m.group('xyz')
> 
> It would be much easier to do:
> 
> if myregex.match(line):
>     xyz = myregex.lastm.group('xyz')
> 
> Having to introduce a new variable name for a match object just
> muddles the code unnecessarily in common regex usage scenarios.

Wouldn't that be a fairly trivial class to write, and use
in your own code?  This is the kind of thing people tend
to whip up on the fly and add to their little bag of tools
if they really like it.

Something like (untested, not real code, just for the idea):

class myregex(re):
     def match(self, data):
         self.lastm = self.pattern.match(data)
         return self.lastm

-Peter



More information about the Python-list mailing list