I am trying to convert a String into proper JSON notation. This string, got some correct indexes (with
[idx]), and some incorrect indexes (with dot notation
.idx. with these last ones starting by 1, instead of by 0). Is there anyway to “handle” captured groups using python
re library or similar?
This is what I have:
import re a = "a..b.1.c" re.sub(r'.(d).', r'..', a) # Wich provides: 'a..b..c'
I would like it to give me
'a..b..c', but I do not know how can I perform operations on the captured group
$1. Is it possible?
The replacer argument of
re.sub can be a function and that function gets passed the match object upon which you can perform operations:
def replacer(mo): captured_group = mo.group(1) one_subtracted = str(int(captured_group) - 1) to_replace = ".[" + one_subtracted + "]." return to_replace
>>> re.sub(r".(d).", replacer, a) 'a..b..c'
mo includes all the to-be-replaced string that your pattern matches, so we put the matched
If you want a
re.sub(r".(d).", lambda mo: ".[" + str(int(mo.group(1))-1) + "].", a)