PHP正则表达式匹配不以“quot;行政人员&引用;登录“;。。。等等

PHP正则表达式匹配不以“quot;行政人员&引用;登录“;。。。等等,php,regex,preg-match,Php,Regex,Preg Match,我搜索了很多,但发现了很多复杂的例子,对我来说太难理解了。无论如何,我试图写下一个正则表达式,它应该尊重: /foo // should match /foo/bar // should match /login // shouldn't match /admin // shouldn't match /admin/foo // shouldn't match /files // shouldn't match 我试过用一个简单的词:\^(\/)([^admi

我搜索了很多,但发现了很多复杂的例子,对我来说太难理解了。无论如何,我试图写下一个正则表达式,它应该尊重:

/foo     // should match
/foo/bar // should match

/login     // shouldn't match
/admin     // shouldn't match
/admin/foo // shouldn't match
/files     // shouldn't match
我试过用一个简单的词:
\^(\/)([^admin])
,它以
/
开头,后面是一些不以
admin
开头的词。它使用
/foo/bar
工作,但使用
/a/foo
失败,因为我想它是以
a
开始的

如何否定一整套单词(
admin
files
login

$pattern = '#^(\/)((?!admin|login).)*$#';


您正在寻找的是所谓的“零宽度断言”或“向前看”和“向后看”@MichaelBerkowski我不行regex是给Symfony路线匹配器的,只接受正则表达式。@Polmonino您应该将该信息编辑到上面的问题中。为什么添加字符串的结尾?与
/不匹配,也与admin
不匹配。@Polmonino:我删除了,但点在第一个之后匹配group@Gumbo:OP请求
/admin
/login
@Akam,以便匹配
/没有管理员
,对吗?
$pattern = '#^(/)((?!admin|login).)(/(.)+)*#';
$array = array(
'/foo',     // should match
'/foo/bar', // should match

'/login',     // shouldn't match
'/admin',     // shouldn't match
'/admin/foo', // shouldn't match
'/files'     // shouldn't match
);

foreach($array as $test){
 if(preg_match($pattern, $test)) echo "Matched :".$test."<br>";
 else echo "Not Matched:".$test."<br>";
}
Matched :/foo
Matched :/foo/bar
Not Matched:/login
Not Matched:/admin
Not Matched:/admin/foo
Matched :/files