p = r'([\,|\.]\d{1}$)'
re.sub(p, r"\1", v)
代码可以运行,但我想在捕获组中添加一个零,而不是替换为捕获组'10',我该怎么做?
re.sub(p, r"\10", v)
失败:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/re.py", line 151, in sub
return _compile(pattern, flags).sub(repl, string, count)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/re.py", line 275, in filter
return sre_parse.expand_template(template, match)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/sre_parse.py", line 802, in expand_template
raise error, "invalid group reference"
sre_constants.error: invalid group reference
[\,|\.]
看起来不对。你是不是想说[.,]
? - georg