Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check for valid domain name in a string?

I am using python and would like a simple regex to check for a domain name's validity. I check at least write domain name.

url = 'https://stackoverflow'
        keyword = 'foo'
        with self.assertRaises(ValueError):
            check_keyword(url, keyword)

I try unit testing on url textfield and there is main.py page where I done the validation main.py-

def check_keyword(url, keyword):

if re.match("^(((([A-Za-z0-9]+){1,63}\.)|(([A-Za-z0-9]+(\-)+[A-Za-z0-9]+){1,63}\.))+){1,255}$" ,url):
   return ValueError("Invalid")

Example

  • www.google (Invalid)

  • https://stackoverflow (Invalid)

like image 930
Vipul Gangwar Avatar asked Jul 10 '17 11:07

Vipul Gangwar


People also ask

How do I check if a domain name is valid?

If you want to find out if a domain name is validated, simply type the URL into the WHOIS database. The search results will also provide you with other crucial information such as who owns it, when it was registered and when it is due to expire.

How do I find the regex for a domain name?

[A-Za-z0-9-]{1, 63} represents the domain name should be a-z or A-Z or 0-9 and hyphen (-) between 1 and 63 characters long. (? <!

How validate hostname in PHP?

php function is_valid_domain_name($domain_name) { return (preg_match("/^([a-z\d](-*[a-z\d])*)(\. ([a-z\d](-*[a-z\d])*))*$/i", $domain_name) //valid chars check && preg_match("/^. {1,253}$/", $domain_name) //overall length check && preg_match("/^[^\.]{ 1,63}(\.


1 Answers

The source of the validators module shows, that this is maybe a little more complex task.

You could use that module:

>>> import validators
>>> validators.domain('example.com')
True

>>> validators.domain('example.com/')
ValidationFailure(func=domain, ...)

Or you can use the RFCs for Domain names to construct your own checker.

like image 131
Christian König Avatar answered Oct 13 '22 20:10

Christian König