Is Perl *that* good?

Duncan Booth me at privacy.net
Wed Apr 28 04:19:31 EDT 2004


wallclimber21 at hotmail.com (Wallclimber) wrote in 
news:cbfb92ae.0404272203.2ff42c98 at posting.google.com:

> I have to agree with the original poster. My *only* complaint about
> Python are not regex search, but regex search and replace operations.
> Perl   : s =~ s/(\w+)\s*=\s*(\d+)/$2 <= $1/g;
> Python : regex.sub(s, "(\w+)\s*=\s*)\d+)", (lambda m: m.group(2)+" <=
> "+m.group(1)))
> 
> I didn't try this out so there might be some syntax problems in there,
> but you get the idea. Is there a 'nicer' way to do this in python?
> Using 'group' and lambda functions is really quite messy. (An,
> obviously, in real life usage, the replacemant function can be much
> bigger...)
> 

Why are you using the deprecated regex module?

The usual way to do this in Python would be:

import re
s = re.sub(r"(\w+)\s*=\s*(\d+)", r"\2 <= \1", s)

Comparing that with the Perl line I prefer the Python way of writing it. 



More information about the Python-list mailing list