相关疑难解决方法(0)

Regex - 匹配除特定字符串之外的任何内容

我需要一个正则表达式(将在ZF2路由中使用,我相信它使用php的preg_match)匹配除特定字符串之外的任何内容.

例如:我需要匹配除"红色","绿色"或"蓝色"之外的任何内容.

我目前有正则表达式:

^(?!red|green|blue).*$

test -> match (correct)
testred -> match (correct)
red -> doesn't match (correct)
redtest -> doesn't match (incorrect)
Run Code Online (Sandbox Code Playgroud)

在最后一种情况下,正则表达式的行为并不像我想要的那样.它应匹配"redtest",因为"redtest"不是("red","green"或"blue").

有关如何修复正则表达式的任何想法?

regex preg-match regex-negation

12
推荐指数
1
解决办法
4万
查看次数

标签 统计

preg-match ×1

regex ×1

regex-negation ×1