一个表达式可能无法实现,但是可以了。
txt = 'check from HERE and i need RED but not BLUE'
对于上面的文本,我需要一个表达式,如果“ RED”出现在“ HERE”之后任何地方,但是“ BLUE”没有出现在此处任何地方,则该表达式将匹配。 / p>
答案 0 :(得分:2)
对于后代:
txt = 'check from HERE and i need RED but not BLUE'
after_here = txt.split('HERE', 1)[1]
result = red in after_here and blue not in after_here
^.*HERE(?!.*BLUE.*).*RED.*$
# ^ look after 'HERE'
# ^ negative lookahead in everything that comes after HEAD for BLUE
# ^ look for RED in everything that comes after HEAD
// see https://www.regular-expressions.info/lookaround2.html