正则表达式除了特定的单词

Agu*_*s P 6 regex exception cpu-word

我有正则表达式的问题.除了一组指定的单词外,我需要制作正则表达式,例如:apple,orange,juice.并且给出这些词语,它将匹配除上述词语之外的所有内容.

applejuice (match)
yummyjuice (match)
yummy-apple-juice (match)
orangeapplejuice (match)
orange-apple-juice (match)
apple-orange-aple (match)
juice-juice-juice (match)
orange-juice (match)

apple (should not match)
orange (should not match)
juice (should not match)
Run Code Online (Sandbox Code Playgroud)

MBO*_*MBO 9

如果您确实希望使用单个正则表达式执行此操作,则可能会发现环绕有用(在此示例中尤其是负向前瞻).为Ruby编写的正则表达式(一些实现具有不同的外观语法):

rx = /^(?!apple$|orange$|juice$)/
Run Code Online (Sandbox Code Playgroud)


Ben*_*Ben -1

类似(PHP)的东西

$input = "The orange apple gave juice";
if(preg_match("your regex for validating") && !preg_match("/apple|orange|juice/", $input))
{
  // it's ok;
}
else
{
  //throw validation error
}
Run Code Online (Sandbox Code Playgroud)