I'm trying to use re.sub
on a string with a numeric value directly after a numeric backreference. That is, if my replacement value is 15.00
and my backreference is \1
, my replacement string would look like:
\115.00
, which as expected will throw an error: invalid group reference
because it thinks my backreference group is 115
.
Example:
import re
r = re.compile("(Value:)([-+]?[0-9]*\.?[0-9]+)")
to_replace = "Value:0.99" # Want Value:1.00
# Won't work:
print re.sub(r, r'\11.00', to_replace)
# Will work, but don't want the space
print re.sub(r, r'\1 1.00', to_replace)
Is there a solution that doesn't involve more than re.sub
?