Check for valid domain name in a string?
17,390
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.
Related videos on Youtube
Author by
Vipul Gangwar
Updated on September 16, 2022Comments
-
Vipul Gangwar over 1 year
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)
-
Marvin Taschenberger almost 7 yearsyou should post your code with what you did / tried so far.
-
Tejas Thakar almost 7 yearsPossible duplicate of Python check if website exists
-
Nathan.Eilisha Shiraini almost 7 yearsPossible duplicate of Domain name validation with RegEx
-
Toto almost 7 yearsWhat is a valid domain name for you? I suggest you to read en.wikipedia.org/wiki/List_of_Internet_top-level_domains
-
user3289695 over 6 yearsI am using that same module and the regular expression that it uses looks weird. Also, it gives this domain as valid: "a.aa*com" and I think it shouldn't be, specially because it has got an asterisk as a part of it.
-
Akash Wankhede almost 4 yearsvalidators.domain("example.com") cannot check for subdomains. So, It wiil return True for validators.domain("abc.example.com") also.
-
Nemo almost 4 yearsThe validators package marks domains with underscores or trailing dots as invalid.