Valid domain name regex


how should be valid domain name regex which full fill following criteria.

  1. each label max 63 characters long minimum 1 characters
  2. contains numbers, letters and ‘-‘, But
  3. should not start and end with ‘-‘
  4. max domain name length 255 characters minimum 1.

for example

some of valid combinations:


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.

Asked By: Nikhil Rupanawar



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.

Answered By: piokuc

Try this:

Answered By: juankysmith

Use the | operator in your RE followed by the ‘-‘.. ensure you escape the literal ‘-‘ with

Answered By: user2878309

Maybe this:

Answered By: adam

Instead of using regex try to look at urlparse

It’s fairly simple to learn and a lot better and comfortable to use.

Answered By: Dropout

and mandatory to end with ‘.’ :
Here i found the solution

Answered By: Nikhil Rupanawar

This expression should meet all the requirements:


  • uses lookahead for total character length
  • domain can optionally end with a .
Answered By: Steve Goossens

You can use a library, e.g. validators. Or you can copy their code:


pip install validators


import validators
if validators.domain('')
    print('this domain is valid')

In the unlikely case you find a mistake, you can fix and report the error.

Answered By: toto_tico
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.