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

Anssi Saari as at
Sun Feb 7 14:33:43 CET 2010

Schif Schaf <schifschaf at> 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