python regular expression replacing part of a matched string

后端 未结 5 747
粉色の甜心
粉色の甜心 2020-12-16 18:57

i got an string that might look like this

\"myFunc(\'element\',\'node\',\'elementVersion\',\'ext\',12,0,0)\"

i\'m currently checking for va

5条回答
  •  臣服心动
    2020-12-16 19:05

    In re.sub, you need to specify a substitution for the whole matching string. That means that you need to repeat the parts that you don't want to replace. This works:

    myRe = re.compile(r"(myFunc\(.+?\,.+?\,)(.+?)(\,.+?\,.+?\,.+?\,.+?\))")
    print myRe.sub(r'\1"noversion"\3', val)
    

提交回复
热议问题