python regular expression replacing part of a matched string

后端 未结 5 740
粉色の甜心
粉色の甜心 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:09

    Read the documentation: re.sub returns a copy of the string where every occurrence of the entire pattern is replaced with the replacement. It cannot in any case modify the original string, because Python strings are immutable.

    Try using look-ahead and look-behind assertions to construct a regex that only matches the element itself:

    myRe = re.compile(r"(?<=myFunc\(.+?\,.+?\,)(.+?)(?=\,.+?\,.+?\,.+?\,.+?\))")
    

提交回复
热议问题