On Sun, Jun 06, 2010 at 06:26:18PM +1000, Steven D'Aprano wrote:
> Two things. Firstly, the Python regex engine numbers backreferences from 
> 1, not 0, so you need \1 and not \0.

Thank for the mail, but i am still not getting it. e.g.


>>> import re
>>> s = 'one two'
>>> re.sub('(one) (two)', r'\1 - \2 \3',s)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.6/re.py", line 151, in sub
    return _compile(pattern, 0).sub(repl, string, count)
  File "/usr/lib/python2.6/re.py", line 278, in filter
    return sre_parse.expand_template(template, match)
  File "/usr/lib/python2.6/sre_parse.py", line 795, in expand_template
    raise error, "invalid group reference"
sre_constants.error: invalid group reference
>>> re.sub('(one) (two)', r'\1 - \2',s)
'one - two'

In first sub I expected,
one two - one two

I understand that \1 is first (group), \2 the second and etc.
But what is the entire regex?

With warm regards,
-Payal
-- 

_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to