检查字符串中的有效域名? [英] 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屋!