Find if the sentence does not contains specific words

谁说胖子不能爱 提交于 2021-01-01 08:12:17

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!