【问题标题】:Regex to validate domains with wildcards正则表达式使用通配符验证域
【发布时间】:2016-03-10 00:20:16
【问题描述】:

我正在使用 PHP 开发 DNS 面板,我必须验证没有尾随点的 DNS 记录名称。以下是几个例子:

example.com - match
sub.example.com - match
sub.sub.example.com - match
*.example.com - match
*.sub.example.com - match
sub.*.example.com - no mach
sub*.example.com - no match
*sub.example.com - no match

我目前正在使用这个正则表达式,但问题是它与通配符 (*) 不匹配:

^(?!\-)(?:[a-z\d\-]{0,62}[a-z\d]\.){1,126}(?!\d+)[a-z\d]{1,63}$

我不太擅长格式化正则表达式。实现这一目标的最佳方法是什么?谢谢!

【问题讨论】:

    标签: php regex dns


    【解决方案1】:

    我找到了一个使用正则表达式的解决方案,它不完全遵守域规则,但效果很好,所以如果您打算使用它,我建议您进行额外检查:

    ^(\*\.)?([a-z\d][a-z\d-]*[a-z\d]\.)+[a-z]+$
    

    有更好的办法通过 PHP 解决这个问题:

    $tmp = $domain_to_check;
    if(strpos($tmp, '*.') === 0){
        $tmp = substr($tmp, 2);
    }
    
    if(filter_var('http://' . $tmp, FILTER_VALIDATE_URL)){
        // The format is valid
    }else{
        // The format is invalid
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-08-18
      • 1970-01-01
      • 2019-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-10
      相关资源
      最近更新 更多