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

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

问题描述

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

<一个href="http://stackoverflow.com/questions/106179/regular-ex$p$pssion-to-match-hostname-or-ip-address">Regular EX pression匹配主机名或IP地址?

在该链接先生提出了一个像样的正则表达式。我有几个问题/问题与:

  • 在Windows计算机/网络名称 如 1abcd 允许(核实 我们的本地网络)
  • 在所提出的正则表达式的点可能只出现一次。我认为 该 abc.def.gh 是一个有效的主机名 还有,是不是。

奇怪,但也没找到可以验证主机名字符串中的任何.NET类(是不是这种情况?)。任何意见将是非常美联社preciated。

更新:任何类/方法的建议 - 请指教东西,将工作无论是在.NET / C#和Silverlight

解决方案
  

在所提出的正则表达式的点可能   只出现一次。我认为   abc.def.gh是一个有效的主机名   好了,是吧。

该点可能会出现不止一次。测试正规前pression 这里,你会看到它匹配。

经常EX pression相关片段(第一部分):

 ([A-ZA-Z0-9] | [A-ZA-Z0-9] [A-ZA-Z0-9 \  - ] * [A-ZA-Z0 -9])\。)*
 

  

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

维基百科

  

的原始规格   主机名在 RFC 952 ,强制要求   标签不能以数字开头或   连字符,并不能以结束   连字符。然而,随后的   规范( RFC 1123 )允许   主机名标签,以数字开始。

我提到 RFC 952 。我会尝试更新常规的前pression的主机名要符合 RFC 1123

Looking for hostname validation regex.

Regular expression to match 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 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.

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.

解决方案

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])\.)*

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

From wikipedia:

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.

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

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

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