如果你想匹配整个句子,你应该使用下面的正则表达式:
^(?!.*Apple).*Peach.*$
演示: https://regex101.com/r/G6RdPO/2/
此外,如果情况不相关,那么您可以将正则表达式更改为:
(?i)^(?!.*Apple).*Peach.*$
最后但并非最不重要的一点是,您的问题不清楚应该如何处理peaches、apples 和pineapples(包含apple)。如果您不想考虑使用它们:
(?i)^(?!.*\bApple\b).*\bPeach\b.*$
演示: https://regex101.com/r/G6RdPO/5
Apple and Peach - Not match
Peach and Apple- Not match
Peach - Match
Three Peach - Match
Peach is good - Match
Peach is good, but Apple is bad - Not match
Nothing - Not match
Peaches are good - Not match
Apples an Peaches - Not match
Pineapple and Peach - Match
如果您还想考虑它们的复数形式:
(?i)^(?!.*\bApple(?:s)?\b).*\bPeach(?:es)?\b.*$
演示: https://regex101.com/r/G6RdPO/4
Apple and Peach - Not match
Peach and Apple- Not match
Peach - Match
Three Peach - Match
Peach is good - Match
Peach is good, but Apple is bad - Not match
Nothing - Not Match
Peaches are good - Match
Apples an Peaches - Not match
Pineapple and Peach - Match