Check for a valid domain name in a string?

后端 未结 5 978
悲哀的现实
悲哀的现实 2020-12-10 04:39

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 actu

5条回答
  •  生来不讨喜
    2020-12-10 05:26

    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).

    So:

    r'[a-zA-Z\d-]{,63}(\.[a-zA-Z\d-]{,63})*'
    

    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?

提交回复
热议问题