Help with regex search-and-replace (Perl to Python)

Anssi Saari as at sci.fi
Sun Feb 7 08:33:43 EST 2010


Schif Schaf <schifschaf at gmail.com> writes:

> (brackets replaced by braces). I can do that with Perl pretty easily:
>
> ~~~~
> for (<>) {
>     s/\[(.+?)\]/\{$1\}/g;
>     print;
> }
> ~~~~

Just curious, but since this is just transpose, then why not simply
tr/[]/{}/? I.e. why use a regular expression at all for this?

In python you would do this with 

for line in text: 
     print line.replace('[', '{').replace(']', '}')




More information about the Python-list mailing list