我需要一个Java正则表达式来匹配除某个单词之外的任何单词,同时包含另一个单词。
例如,字符串不能包含Apple,但必须具有Peach。
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也就是说,Apple 和 Peach不能出现在字符串中。
到目前为止我已经有了这个密码。
^(?!(?:Apple)$)Peach$发布于 2019-01-08 02:22:07
如果要匹配整个句子,应使用以下正则表达式:
^(?!.*Apple).*Peach.*$演示: https://regex101.com/r/G6RdPO/2/
此外,如果情况与此无关,则可以将regex更改为:
(?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发布于 2019-01-08 02:07:21
https://stackoverflow.com/questions/54084335
复制相似问题