Is Perl *that* good?

Peter Otten __peter__ at web.de
Wed Apr 28 03:01:20 EDT 2004


Wallclimber wrote:

> 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?

Your example suggests that the Python equivalent for $1 is \1:

>>> re.sub(r"(\w+)\s*=\s*(\d+)", r"\2 <= \1", "a=1 b = 3 c = d")
'1 <= a 3 <= b c = d'

Peter



More information about the Python-list mailing list