问题
i have a regular expression which find if the sentences contains specific words
my query is:
(?=.*hello)(?=.*hi)(?=.*hey).*
but now i want to check if my sentence does not contains this words
i have tried:
(?=.*((?!hello).))(?=.*((?!hi).))(?=.*((?!hey).)).*
but it does not works
how should i build my query?
Example:
this query should return true when my sentence is:
hi, how are you?
and must return false when my sentence is:
hi hello hey ..
thanks in advance,
回答1:
If the problem is to match the string if the three words/patterns don't appear in the string together, then here is a simpler solution to the problem:
^(?!(?=.*hello)(?=.*hi)(?=.*hey)).*
Or an alternative solution (by De Morgan's law):
^(?:(?!.*hello)|(?!.*hi)|(?!.*hey)).*
(not(A and B and C) is equivalent to not(A) or not(B) or not (C))
Note that it effectively scans the pattern as many times as the number of words to be checked, and it again runs into permutation problem if you want to check whether k out of n words appear in the string.
来源:https://stackoverflow.com/questions/32201079/find-if-the-sentence-does-not-contains-specific-words