does \1 in re work?

Michael P. Soulier msoulier at
Wed Nov 14 23:10:08 CET 2001


    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,


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

More information about the Python-list mailing list