Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Numeric value directly after backreference [duplicate]

Tags:

python

regex

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?

like image 812
atp Avatar asked Jun 27 '16 22:06

atp


1 Answers

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 group 20, 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
like image 181
Wiktor Stribiżew Avatar answered Oct 03 '22 13:10

Wiktor Stribiżew