using re module to find
anton
antonxx at gmx.de
Fri Jun 13 04:23:41 EDT 2008
John Machin <sjmachin <at> lexicon.net> writes:
>
> On Jun 12, 7:11 pm, anton <anto... at gmx.de> wrote:
> > Hi,
> >
> > I want to replace all occourences of " by \" in a string.
> >
> > But I want to leave all occourences of \" as they are.
> >
> > The following should happen:
> >
> > this I want " while I dont want this \"
... cut text off
> What you want is:
>
> >> import re
> >> text = r'frob this " avoid this \", OK?'
> >>> text
> 'frob this " avoid this \\", OK?'
> >> re.sub(r'(?<!\\)"', r'\"', text)
> frob this \\" avoid this \\", OK?'
> >>
>
> HTH,
> John
> --
> http://mail.python.org/mailman/listinfo/python-list
>
>
First.. thanks John.
The whole problem is discussed in
http://docs.python.org/dev/howto/regex.html#the-backslash-plague
in the section "The Backslash Plague"
Unfortunately this is *NOT* mentioned in the standard
python documentation of the re module.
Another thing which will always remain strange to me, is that
even if in the python doc of raw string:
http://docs.python.org/ref/strings.html
its written:
"Specifically, a raw string cannot end in a single backslash"
s=r"\\" # works fine
s=r"\" # works not (as stated)
But both ENDS IN A SINGLE BACKSLASH !
The main thing which is hard to understand is:
If a raw string is a string which ignores backslashes,
then it should ignore them in all circumstances,
or where could be the problem here (python parser somewhere??).
Bye
Anton
More information about the Python-list
mailing list