首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >regex -排除子域(preg_match)

regex -排除子域(preg_match)
EN

Stack Overflow用户
提问于 2013-03-06 11:00:45
回答 3查看 2.5K关注 0票数 3

我想匹配一个域(与preg_match),但我想排除一个子域。

示例我希望匹配example.org上的所有子域,但boon.example.org除外

我试过这个:

代码语言:javascript
复制
$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

有人有答案吗?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-03-06 11:37:58

如果你只在寻找精确的子域,你就不能检查一下字符串boon.example.org是否存在吗?这看起来有点太过分了。

无论如何,下面的regex应该做您想做的事情:

代码语言:javascript
复制
.*(?<!\bboon\.|^.)example.org

将返回所有子域的subdomain.example.org,但boon.example.orgboon.example.org的任何子域除外。

票数 4
EN

Stack Overflow用户

发布于 2013-03-06 11:05:46

试试这个:

代码语言:javascript
复制
echo '<pre>';
$test = "boon.example.org";
$test2 = "null";
preg_match('/^(?!boon\.)([\w]+\.)?example\.org$/', $test, $output);
print_r($output);

这将匹配除恩恩以外的所有子域。

票数 0
EN

Stack Overflow用户

发布于 2013-03-06 11:12:32

这个regex适用于您:

代码语言:javascript
复制
^(((?!boon).)+\.)?example\.org$

RegExr链路

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15245531

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档