Python re expr from Perl to Python
Carsten Haese
carsten at uniqsys.com
Sat Jan 6 11:15:18 EST 2007
On Sat, 2007-01-06 at 15:43 +0100, Michael M. 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?
Here's an almost literal translation:
##################################################
import re
na = re.sub(r"\ \|(.*?)\ \|(.*?)\ \|", r"\2", na)
na = na.replace("[", "")
na = na.replace("]", "")
##################################################
Background information on regular expressions in Python can be found
here:
http://www.amk.ca/python/howto/regex/
http://docs.python.org/lib/module-re.html
Hope this helps,
Carsten.
More information about the Python-list
mailing list