寻找主机名/机器名验证的正则表达式/代码
寻找主机名验证正则表达式。
链接先生们提出了一个不错的正则表达式。我有一些问题/疑问:
- 在 Windows 计算机/网络名称上 像
1abcd
是允许的(已验证 我们的本地网络) - 在提议的正则表达式中,点可能只出现一次。我假设
abc.def.gh
是有效的主机名 也是,不是吗。
奇怪,但也找不到任何可以验证主机名字符串的.NET 类(是这种情况吗?)。任何建议将不胜感激。
更新:对于任何类/方法提案 - 请建议一些在 .NET/C# 和 SilverLight 中都适用的内容。
Looking for hostname validation regex.
Regular expression to match DNS hostname or IP Address?
In that link gentlemen propose a decent regex. I have a few problems/questions with that:
- On windows computers/networks names
like1abcd
are allowed (verified on
our local network) - In the proposed regex the dot might appear only once. I'd assume
thatabc.def.gh
is a valid hostname
as well, isn't it.
Strange, but also couldn't find any .NET class that can validate hostname string (is it the situation?). Any advice will be highly appreciated.
Update: for any class/method proposal - please advice something that will work both in .NET/C# and SilverLight.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
该点可能会出现多次。在此处测试正则表达式,您将看到它匹配。
正则表达式的相关片段(第一部分是):
来自 wikipedia:
我参考了 RFC 952。我将尝试更新主机名的正则表达式,使其符合 RFC 1123。
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):
From wikipedia:
I referred to RFC 952. I will try to update the regular expression for hostnames to be compliant with RFC 1123.
Uri.CheckHostName 是可以帮助您的方法吗?
如果主机名无法识别,结果将为“未知”。
Is Uri.CheckHostName a method that can help you ?
If the hostname is not recognize, the result will be "Unknown".
(?i)(?=.{5,20}$)^(([az\d]|[az\d][az\d\-]*[az\d])\.)* ([az\d]|[az\d][az\d\-]*[az\d])$
说明:
(?i)
- 忽略大小写(?=.{5,20}$)
- 将字符串长度限制在 5 到 20 个字符内。<代码>^(([az\d]|[az\d][az\d\-]*[az\d])\.)*([az\d]|[az\d][az\ d\-]*[az\d])$ - 接受任何字母和数字以及连字符和点
(?i)(?=.{5,20}$)^(([a-z\d]|[a-z\d][a-z\d\-]*[a-z\d])\.)*([a-z\d]|[a-z\d][a-z\d\-]*[a-z\d])$
Explanation:
(?i)
- Ignore case(?=.{5,20}$)
- Limit the string length within 5 to 20 characters.^(([a-z\d]|[a-z\d][a-z\d\-]*[a-z\d])\.)*([a-z\d]|[a-z\d][a-z\d\-]*[a-z\d])$
- Accepts any alphabets and numeric along with hyphen and dot