我想匹配一个域(与preg_match),但我想排除一个子域。
示例我希望匹配example.org上的所有子域,但boon.example.org除外
我试过这个:
$test = "boon.example.org";
$test2 = "null";
if(preg_match('#(?!boon)(\w+\.)?example\.org#', $test, $output)) {
$test2 = $output[2] .'example.org';
}但是test2的输出是: oon.example.org而不是example.org
有人有答案吗?
发布于 2013-03-06 11:37:58
如果你只在寻找精确的子域,你就不能检查一下字符串boon.example.org是否存在吗?这看起来有点太过分了。
无论如何,下面的regex应该做您想做的事情:
.*(?<!\bboon\.|^.)example.org将返回所有子域的subdomain.example.org,但boon.example.org或boon.example.org的任何子域除外。
发布于 2013-03-06 11:05:46
试试这个:
echo '<pre>';
$test = "boon.example.org";
$test2 = "null";
preg_match('/^(?!boon\.)([\w]+\.)?example\.org$/', $test, $output);
print_r($output);这将匹配除恩恩以外的所有子域。
发布于 2013-03-06 11:12:32
这个regex适用于您:
^(((?!boon).)+\.)?example\.org$RegExr链路
https://stackoverflow.com/questions/15245531
复制相似问题