[Tutor] Simple regex replacement match (converting from Perl)

GoodPotatoes goodpotatoes at yahoo.com
Fri Sep 4 19:34:51 CEST 2009


I simply want to remark out all non-word characters read from a line.

Line:
Q*bert says "#@!$%  "

in Perl
#match each non-word character, add "\" before it, globally.

$_=s/(\W)/\\$1/g;

output:
Q\*bert\ says\ \"\#\@\!\$\%\ \ \"  #perfect!  

Is there something simple like this in python?

I would imagine:
foo='Q*bert says "#@!$%  "'
pNw=re.compile('(\W)')
re.sub(pNw,'\\'+(match of each non-word character),foo)

How do I get the match into this function?  Is there a different way to do this?


      
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20090904/1f80541c/attachment.htm>


More information about the Tutor mailing list