does \1 in re work?

Justin Dubs jtdubs at eos.ncsu.edu
Wed Nov 14 23:40:18 CET 2001


"Michael P. Soulier" <msoulier at nortelnetworks.com> wrote in message
news:slrn9v5qvc.u31.msoulier at pmerd071.ca.nortel.com...
>     Hello.
>
>     Looking at the documentation for python 1.5.2, the now aging version
that
> I'm using, in the re module it states that the standard \1, \2 keys that
> reference that matched value in parenthesis in the pattern do in fact
work, as
> do \g<1>, \g<2>, etc.
>
>     So, I tried this:
>
>     new = re.sub("^(-?\d+)(\d{3})", '\1,\2', amount)
>
>     new always ended up being '\001,\002'. Not what I wanted.
>
>     So, I used the alternate...
>
>     new = re.sub("^(-?\d+)(\d{3})", '\g<1>,\g<2>', amount)
>
>     Which worked perfectly, giving me, for example,
>
>     '100000,000' where amount was '100000000'.
>
>     So, my question is, what was wrong with my first pattern?
>
>     Thanks very much,
>
>     Mike
>
> --
> Michael P. Soulier, TD12, SKY  Tel: 613-765-4699 (ESN: 39-54699)
> Optical Networks, Nortel Networks, SDE Pegasus
> "...the word HACK is used as a verb to indicate a massive amount
> of nerd-like effort."  -Harley Hahn, A Student's Guide to Unix

Python is getting to your strings before re.sub() is.  It's changing the \1
and \2 into the corresponding characters before re.sub() ever sees the
string because the \'s are escape characters.  Two easy solutions, offhand:

re.sub(...., r"\1,\2", ...)

Notice the r in front of the quotation marks.  This will stop python from
messing with those \'s.

re.sub(...., "\\1,\\2", ...)

In this case, the change is the double \'s which will be processed by Python
and turned into the single \'s that you wanted originally.

Either way, it'll work.  Have fun and good luck,

Justin





More information about the Python-list mailing list