Looking for Python equivalent to a Perl'ism

Ixokai ixokai-nf at myNOSPAM.org
Thu Mar 29 22:45:26 CEST 2001


While this is not exactly what you want, were you aware of the following
syntax:

dict = {"foo":"yechfoo", "mumble":"yukkomumble"}

print "This is %(foo)s this is junk %(mumble)s and more stuff" % dict

--Stephen
(replace 'NOSPAM' with 'seraph' to respond)

David Lees wrote in message <3AC375A7.A4BA87F1 at spammraqia.com>...
>
>I am trying to figure out how to do the Python equivalent of the
>following perl code, which finds all the groups tagged by 'xx' and
>replaces them with text looked up in a dictionary:
>----------
>my %dict;
>my $string="this is xxfooxx this is junk xxmumblexx and more stuff";
>%dict=(foo=>"yechfoo",mumble=>"yukkomumble");
>
>$string =~ s/xx(\S+)xx/$dict{$1}/g;
>
>print $string;
>---------------------
>
>My Python equivalent without the backreference dictionary lookup is:
>
>dict ={'xxx':'foo',
>       'yyy':'barf',
>       }
>p=re.compile(r'zz(\w+)zz')
>s='this is and so is zzxxxzz abc that an zzyyyzz abc zzxxxzz or else'
>print re.sub(p,'junk',s)
>
>--------------
>
>But I do not understand how to include backreferences into the regex
>substitution as is conveniently done in perl.  Can someone tell me if
>there is an easy Python solution to this?
>
>Thanks in advance.
>
>David Lees




-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----==  Over 80,000 Newsgroups - 16 Different Servers! =-----



More information about the Python-list mailing list