does \1 in re work?

Justin Dubs jtdubs at eos.ncsu.edu
Wed Nov 14 18:23:56 EST 2001


"Justin Dubs" <jtdubs at eos.ncsu.edu> wrote in message
news:9surpa$eek$1 at uni00nw.unity.ncsu.edu...
> "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
>
>

Oh, just a nitpick, but your re.sub only works if you need to add one comma
to the number.  If you want a quick function that will work on arbitrary
numbers than here's one I whipped up real quick.   I couldn't figure out how
to do it in a single regex, but then, I'm not that good with regex's.

>>>import re
>>>def addcommas(s):
...    while(1):
...        s, c = re.subn("(\d)(\d{3})((,\d{3})*)$", r"\1,\2\3", s)
...        if c==0: break
...    return s
...
>>> addcommas("10000000000")
'10,000,000,000'
>>>^Z

I tested it for the first couple power of ten and it seems to work fine.
Anyway, hehe, I was bored.  Have fun,

Justin Dubs





More information about the Python-list mailing list