regex-排除子域(Preg_Match) [英] regex - exclude a subdomain (preg_match)

查看:22
本文介绍了regex-排除子域(Preg_Match)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想匹配一个域(带有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

有人有答案吗?

推荐答案

如果您只查找确切的子域,难道不能只检查字符串boon.example.org是否存在吗?对于这一点,使用正则表达式似乎有点过头了。

无论如何,以下正则表达式应该可以执行您想要的操作:

.*(?<!boon.|^.)example.org

将为boon.example.org以外的所有子域或boon.example.org的任何子域返回subdomain.example.org

这篇关于regex-排除子域(Preg_Match)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆