寻找主机名/机器名验证的正则表达式/代码 [英] Looking for regex/code for hostname/machine name validation

查看:288
本文介绍了寻找主机名/机器名验证的正则表达式/代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

寻找主机名验证正则表达式。

Looking for hostname validation regex.

正则表达式匹配DNS主机名或IP地址?

在那个连接中,绅士提出了一个体面的正则表达式。我有一些问题/问题:

In that link gentlemen propose a decent regex. I have a few problems/questions with that:


  • 在Windows计算机/网络名称
    喜欢 1abcd 被允许(在本地网络的
    上验证)

  • 在提议的正则表达式中,点可能只会出现一次。我假设
    abc.def.gh 也是一个有效的主机名
    ,不是。

  • On windows computers/networks names like 1abcd are allowed (verified on our local network)
  • In the proposed regex the dot might appear only once. I'd assume that abc.def.gh is a valid hostname as well, isn't it.

奇怪,但也找不到可以验证主机名字符串的任何.NET类(是情况吗?任何建议将被高度赞赏。

Strange, but also couldn't find any .NET class that can validate hostname string (is it the situation?). Any advice will be highly appreciated.

更新:对于任何类/方法提案 - 请提供一些在.NET / C#和SilverLight。

Update: for any class/method proposal - please advice something that will work both in .NET/C# and SilverLight.

推荐答案


在提议的正则表达式中,点可能
只出现一次。我假设
abc.def.gh是一个有效的主机名,如
好​​,不是这样。

In the proposed regex the dot might appear only once. I'd assume that abc.def.gh is a valid hostname as well, isn't it.

点可能会出现多次。测试正则表达式此处,您将看到它匹配。

The dot MAY appear more than once. Test the regular expression here and you will see that it matches.

正则表达式的相关片段(第一部分是):

Relevant fragment of the regular expression (first part is):

([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*




在Windows计算机/网络名称
像1abcd被允许(在
上验证我们的本地网络)

On windows computers/networks names like 1abcd are allowed (verified on our local network)

维基百科


主机名的原始规范.org / html / rfc952rel =nofollow> RFC 952 ,强制
标签无法以数字开头,或
以连字符开头,不得以$ b结尾$ b连字符。然而,随后的
规范( RFC 1123 )允许
主机名标签以数字开头。

The original specification of hostnames in RFC 952, mandated that labels could not start with a digit or with a hyphen, and must not end with a hyphen. However, a subsequent specification (RFC 1123) permitted hostname labels to start with digits.

我参考了 RFC 952 。我将尝试更新主机名的正则表达式以符合 RFC 1123

I referred to RFC 952. I will try to update the regular expression for hostnames to be compliant with RFC 1123.

这篇关于寻找主机名/机器名验证的正则表达式/代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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