主机名正则表达式 [英] The Hostname Regex
问题描述
我正在寻找 正则表达式来验证主机名.它必须完全符合标准.现在,我有
I'm looking for the regex to validate hostnames. It must completely conform to the standard. Right now, I have
^[0-9a-z]([0-9a-z\-]{0,61}[0-9a-z])?(\.[0-9a-z](0-9a-z\-]{0,61}[0-9a-z])?)*$
但它允许连续的连字符和超过 255 个字符的主机名.如果完美的正则表达式是不可能的,请直说.
but it allows successive hypens and hostnames longer than 255 characters. If the perfect regex is impossible, say so.
编辑/澄清:Google 搜索未显示这是一个已解决(或已证明无法解决)的问题.我想创建明确的正则表达式,这样就没有人必须自己编写正则表达式了.如果方言很重要,我希望每个方言都有一个版本可以做到这一点.
Edit/Clarification: a Google search didn't reveal that this is a solved (or proven unsolvable) problem. I want to to create the definitive regex so that nobody has to write his own ever. If dialects matter, I want a a version for each one in which this can be done.
推荐答案
^(?=.{1,255}$)[0-9A-Za-z](?:(?:[0-9A-Za-z]|-){0,61}[0-9A-Za-z])?(?:\.[0-9A-Za-z](?:(?:[0-9A-Za-z]|-){0,61}[0-9A-Za-z])?)*\.?$
这篇关于主机名正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!