Python regex: substitute all occurrence of a certain string NOT after a specific character

前端 未结 2 1855
猫巷女王i
猫巷女王i 2021-01-27 07:51

How can I substitute all occurrence of a certain string NOT after a specific character in Python?

For example, I want to substitute all occurrence of abc NO

2条回答
  •  情歌与酒
    2021-01-27 08:43

    result = re.sub("(?
    • The negative lookbehind (? asserts that what precedes is not x
    • abc matches abc
    • We replace with def

    Reference

    • Lookahead and Lookbehind Zero-Length Assertions
    • Mastering Lookahead and Lookbehind

提交回复
热议问题