Trying to use regex to perform substitutions on a string.
re.sub(regex, repl_func, content)
The problem is that I do not understand how the replacement will occur via the repl_func
when I need the substitution to be dependent on the matching object from that regex
; i.e. I will use that matched object as a dictionary key to get the matching value with which that particular object needs to be replaced.
for mobj in re.finditer(regex, content):
t = mobj.lastgroup
v = match_obj.group(t)
if t == 'NAME':
...
elif t == '
':
...
I have tried to iterate all the matching objects, as seen above, but cannot figure out how to apply the re.sub. If I understand correctly, the repl_function
in my first code segment needs to somehow know which is the matched object from that regex.
Any ideas, especially using code will be much appreciated cause I am only just now starting with Python.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…