[英]How to replace a specific character after the occurrence of a string using regex
[英]Python regex: substitute all occurrence of a certain string NOT after a specific character
如何在Python中的某個特定字符之后而不是某個字符串的所有出現位置替換?
例如,我想用def
代替所有出現的abc
NOT替換為x
。 這是代碼:
re.sub(r'(^|[^x])abc', r'\1def', string)
如果字符串沒有連續的abc
,則代碼可以正常工作。 但是,如果我有string='abcabc'
,則代碼將無法工作。 有什么辦法解決這個問題?
result = re.sub("(?<!x)abc", "def", subject)
(?<!x)
斷言前面不是x
abc
匹配abc
def
代替 參考
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.