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
?
Use an unambiguous backreference syntax \g<1>
. See re.sub
reference:
\g<number>
uses the corresponding group number;\g<2>
is therefore equivalent to\2
, but isn’t ambiguous in a replacement such as\g<2>0
.\20
would be interpreted as a reference to group20
, not a reference to group 2 followed by the literal character '0'.
See this regex demo.
Python demo:
import re
r = re.compile("(Value:)([-+]?[0-9]*\.?[0-9]+)")
to_replace = "Value:0.99" # Want Value:1.00
print(re.sub(r, r'\g<1>1.00', to_replace))
# => Value:1.00
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With