Python equivalent of Perl e flag with regular expression

Mark Thomas mark at thomaszone.com
Thu Oct 2 16:49:02 EDT 2008


On Oct 2, 4:03 pm, s... at pobox.com wrote:
>     Jason> With Perl I might do something like this:
>     Jason> $line =~ s/(select)/uc($1)/e;
>     ...
>     Jason> How would I do this with Python?
>
> I'm sure there are plenty of ways to skin this particular cat, but how is
> 's/.../.../e' different from 's/.../.../'?  (For those of us who are not
> Perl mongers.)
>
> Skip

The '/e' means that the replacement string is actually code to be
executed, the output of which becomes the replacement string.

Perl even allows '/ee' which executes the code, then takes the output
of that and executes it again. In fact, you can have an arbitrary
number of e's, which is very handy for crazy people.

-- Mark.



More information about the Python-list mailing list