using re module to find
John Machin
sjmachin at lexicon.net
Fri Jun 13 07:23:38 EDT 2008
On Jun 13, 6:23 pm, anton <anto... at gmx.de> wrote:
> 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.
Yes, and there's more to driving a car in heavy traffic than you will
find in the manufacturer's manual.
>
> 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 !
Apply the interpretation that the first case ends in a double
backslash, and move on.
>
> 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,
Nobody defines a raw string to be a "string that ignores backslashes",
so your premise is invalid.
> or where could be the problem here (python parser somewhere??).
Why r"\" is not a valid string token has been done to death IIRC at
least twice in this newsgroup ...
Cheers,
John
More information about the Python-list
mailing list