检查字符串中的有效域名? [英] Check for a valid domain name in a string?
问题描述
我正在使用 python 并且想要一个简单的 api 或正则表达式来检查域名的有效性.根据有效性,我指的是句法有效性,而不是域名是否在互联网上实际存在.
I am using python and would like a simple api or regex to check for a domain name's validity. By validity I am the syntactical validity and not whether the domain name actually exists on the Internet or not.
推荐答案
如果域名是以点分隔的标识符列表,每个域名不超过 63 个字符,并且由字母、数字和破折号(无下划线).
Any domain name is (syntactically) valid if it's a dot-separated list of identifiers, each no longer than 63 characters, and made up of letters, digits and dashes (no underscores).
所以:
r'[a-zA-Z\d-]{,63}(\.[a-zA-Z\d-]{,63})*'
将是一个开始.当然,现在可能允许使用一些非 Ascii 字符(最近的一项发展),这会极大地改变参数——您需要处理吗?
would be a start. Of course, these days some non-Ascii characters may be allowed (a very recent development) which changes the parameters a lot -- do you need to deal with that?
这篇关于检查字符串中的有效域名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!