tags:

views:

84

answers:

2

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

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

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

+4  A: 

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).*$
Mark Byers
@Mark Byers but i need to use ir in .htaccess. will it work by ^(?!.*word1)(?!.*word2)(?!.*word3).*$?
Syom
Mark Byers
@Mark Byers thanks much Mark. but it's new for me! i neve do such things by so.could you explain why ? and . and * in (?!.*word1), and where you "say" 'none of those words'?
Syom
@Syom If this is just for .htaccess, the question should not be tagged PHP. There are tags for htaccess and mod_rewrite, those might be more apt.
Heikki Naski
A: 

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.

Philipp Andre