Is Perl *that* good?
Carl Banks
imbosol at aerojockey.invalid
Wed Apr 28 23:20:16 EDT 2004
Peter Otten wrote:
> 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'
You can do that? LSNED.
--
CARL BANKS http://www.aerojockey.com/software
"If you believe in yourself, drink your school, stay on drugs, and
don't do milk, you can get work."
-- Parody of Mr. T from a Robert Smigel Cartoon
More information about the Python-list
mailing list