Python for web...

Larry Bates larry.bates at websafe.com
Mon Feb 25 12:34:30 EST 2008


thushianthan15 at gmail.com wrote:
> 
> In Perl, it was:
> 
> 
>   ## Example: "Abc | def | ghi | jkl"
>   ##       -> "Abc ghi jkl"
>   ## Take only the text betewwn the 2nd pipe (=cut the text in the 1st 
> pipe).
>   $na =~ s/\ \|(.*?)\ \|(.*?)\ \|/$2/g;
> 
>   ## -- remove [ and ] in text
>   $na =~ s/\[//g;
>   $na =~ s/\]//g;
>   # print "DEB: \"$na\"\n";
> 
> 
> # input string
> na="Abc | def | ghi | jkl [gugu]"
> # output
> na="Abc ghi jkl gugu"
> 
> 
> How is it done in Python?

You don't really need regular expressions for this simple transformation:

na="Abc | def | ghi | jkl [gugu]"

na=" ".join([x.strip() for x in na.replace("[","|").replace("]","").split("|")])

-Larry



More information about the Python-list mailing list