text='AND(AA6<AI6,AG7>BA10)' # original text # Replace dictionary list = { "AA":" Xiao Ming ","AG":" Xiao Wang ",}# We hope to achieve the following results text_new='AND( Xiao Ming <AI6, Xiao Wang >BA10)' # Exact match only , It is impossible to put Xiao Ming , The number after Xiao Wang is replaced by for item in list: text_new = score_formula.replace(item,list[item])#text_new='AND( Xiao Ming 6<AI6, Xiao Wang 7>BA10)'# I try to write regular , But it failed for item in list: reg=re.compile(list[item] +"d+") match=reg.search(text)
How to optimize ??