what is wrong with that r"\"

Nick Craig-Wood nick at craig-wood.com
Thu Jul 5 04:30:03 EDT 2007


Marc 'BlackJack' Rintsch <bj_666 at gmx.net> wrote:
>  On Wed, 04 Jul 2007 11:21:14 +0000, Neil Cerutti wrote:
> 
> > If the escaped quotes didn't function in raw strings, I'd be
> > unable to construct (with a single notation) a regex that
> > included both kinds of quotes at once.
> > 
> >   re.compile(r"'\"")
> 
>  Where's the problem!? ::
> 
>    re.compile(r''''"''')
> 
>  Ah, I see -- readability is the problem.  :-)

Actually the problem is that those backslashes don't actually
disappear thus producing non-intuititive behaviour.  The example you
provided produces a different result to Neil's result.

  >>> r"'\""
  '\'\\"'
  >>> r''''"'''
  '\'"'
  >>> 

Neil is correct in saying that his example works for regexp matching
though, as the regexp matcher understands \" as being the same as ".

So r"" strings work well as Regexp-strings but not so well as
Raw-strings IMHO.

-- 
Nick Craig-Wood <nick at craig-wood.com> -- http://www.craig-wood.com/nick



More information about the Python-list mailing list