Reputation: 4191
how should be valid domain name regex which full fill following criteria.
for example
some of valid combinations:
a
a.com
aa-bb.b
I created this ^(([a-z0-9]){1,63}\.?){1,255}$
But currently its not validating '-' part as required (it's , missing)
Is there any way?
plz correct me if I am wrong.
Upvotes: 2
Views: 26179
Reputation: 19027
You can use a library, e.g. validators. Or you can copy their code:
pip install validators
import validators
if validators.domain('example.com')
print('this domain is valid')
In the unlikely case you find a mistake, you can fix and report the error.
Upvotes: 2
Reputation: 13866
Instead of using regex try to look at urlparse
https://docs.python.org/3/library/urllib.parse.html
It's fairly simple to learn and a lot better and comfortable to use.
Upvotes: 0
Reputation: 1028
This expression should meet all the requirements:
^(?=.{1,255}$)(?!-)[A-Za-z0-9\-]{1,63}(\.[A-Za-z0-9\-]{1,63})*\.?(?<!-)$
.
Upvotes: 2
Reputation: 4191
and mandatory to end with '.' : Here i found the solution
"^(((([A-Za-z0-9]+){1,63}\.)|(([A-Za-z0-9]+(\-)+[A-Za-z0-9]+){1,63}\.))+){1,255}$"
Upvotes: 3
Reputation: 26184
Don't use regex for parsing domain names, use urllib.parse.
If you need to find valid domain names in HTML then split the text of the page with a regex [ <>]
and then parse each resulting string with urllib.parse.
Upvotes: 0
Reputation: 131
Use the | operator in your RE followed by the '-'.. ensure you escape the literal '-' with \
Upvotes: 0