opensubscriber
   Find in this group all groups
 
Unknown more information…

p : python-list@python.org 27 October 2011 • 4:26AM -0400

Re: inserting \ in regular expressions
by Ian Kelly

REPLY TO AUTHOR
 
REPLY TO GROUP




On Wed, Oct 26, 2011 at 1:48 PM, Ross Boylan <ross@bios...> wrote:
> Or perhaps I'm confused about what the displayed results mean.  If a
> string has a literal \, does it get shown as \\?

In the repr, yes.  If you try printing the string, you'll see that it
only contains one \.

By the way, regular expressions are overkill here.  You can use the
str.replace method for this:

>>> print(r'Silly \ " quote'.replace('\\', '\\\\').replace('"', r'\"'))
Silly \\ \" quote

Cheers,
Ian
--
http://mail.python.org/mailman/listinfo/python-list

Bookmark with:

Delicious   Digg   reddit   Facebook   StumbleUpon

Related Messages

opensubscriber is not affiliated with the authors of this message nor responsible for its content.