尝试使用负向前瞻来替换所有不符合模式的字符串:
regexPattern = '((?!*' + 'word1|word2|word3' + ').)*$'
mytext= 'jsdjsqd word1dsqsqsword2fjsdjswrod3sqdq'
return re.sub(regexPattern, "P", mytext)
#Expected Correct Output: 'PPPPPPword1PPPPPPword2PPPPPword3PPP'
#BAD Output: 'jsdjsqd word1dsqsqsword2fjsdjswrod3sqdq'
我尝试了这个,但它没有起作用(字符串仍然相同)。 如何修改它?(我认为这是非常困难的正则表达式)
word1
жҲ–word2
жҲ–word3
зҡ„еӯ—з¬ҰдёІеҗ—пјҹr'(?s)^(?!.*(?:word1|word2|word3)).*$'
гҖӮе®ғдёҚиғҪеҫҲеҘҪең°е·ҘдҪң - е®ғеҜ№дҪ жқҘиҜҙжҳҜеҰӮдҪ•е·ҘдҪңзҡ„пјҢжңүд»Җд№Ҳй—®йўҳпјҹ - Wiktor Stribiżew