开发者

regex: find the part, which doesn't contain none of some words

how can i much the sentense, if it doesn't contain none of {word1,word2,word3} where i must 开发者_JAVA百科put ^ symbol? i think it must looks like this

^([^word1|word2|word3])$

but it doesn't work. could you help? thanks


Regex isn't the best tool for testing these sorts of conditions, but if you must then you can do it with negative lookaheads:

^(?!.*word1)(?!.*word2)(?!.*word3).*$

What you are trying to do won't work because [^...] is a negative character class with an unordered list of characters. What you wrote is equivalent to:

^([^123dorw|])$

Note also that depending on your needs you might also want to include word-boundaries in your regular expression:

^(?!.*\bword1\b)(?!.*\bword2\b)(?!.*\bword3\b).*$


im not familiar with the use of regex in htaccess, so my thoughts may be bit high level:

what you try looks like kind of: if sentence contains not word1 | not word2 | not word3 then do something

i would suggest a solution the way: if sentence contains word1|word2|word3 then do nothing else do something

means don't use the negation in the "query", but in the result, which makes the regex simpler.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜