does \1 in re work?

Steve Holden sholden at holdenweb.com
Wed Nov 14 17:34:02 EST 2001


"Michael P. Soulier" <msoulier at nortelnetworks.com> wrote...
>     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?
>
The fact that you have neither used a raw string nor escaped the
backslashes. Since \g isn't a recognised escape you get away with that! Try

    new = re.sub("^(-?\d+)(\d{3})", r'\1,\2', amount)

and see what difference it makes.

regards
 Steve
--
http://www.holdenweb.com/








More information about the Python-list mailing list