我正在尝试替换字符串中特定数字的所有出现。例如,假设我想用另一个替换给定数字的特定实例:
>>> number1 = 33
>>> number2 = 1
>>> re.sub('(foo)%i' % number1, '\\1%i' % number2, 'foo33')
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "/home/david_clymer/Development/VistaShare/ot_git/lib/python2.4/sre.py", line 142, in sub
return _compile(pattern, 0).sub(repl, string, count)
File "/home/david_clymer/Development/VistaShare/ot_git/lib/python2.4/sre.py", line 260, in filter
return sre_parse.expand_template(template, match)
File "/home/david_clymer/Development/VistaShare/ot_git/lib/python2.4/sre_parse.py", line 784, in expand_template
raise error, "invalid group reference"
sre_constants.error: invalid group reference
>>> re.sub('(foo)%i' % number1, '\\1 %i' % number2, 'foo33')
'foo 1'
如何防止组参考与以下数字混为一谈?