Domain Name in PY
Match fully-qualified domain names like example.com or api.sub.example.co.uk.
Try it in the PY tester →Pattern
regexPY
(?:[a-zA-Z0-9](?:[a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,} (flags: g)Python (re) code
pyPython
import re
pattern = re.compile(r"(?:[a-zA-Z0-9](?:[a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}")
input_text = "example.com"
for m in pattern.finditer(input_text):
print(m.group(0))Stdlib `re` module — no third-party dependency. Works on Python 3.6+.
How the pattern works
Each label is 1–63 characters of letters, digits, or hyphens (not starting or ending with hyphen). One or more labels followed by a TLD of 2+ letters.
Examples
Input
example.comMatches
example.com
Input
api.sub.example.co.ukMatches
api.sub.example.co.uk
Input
not a domainNo match
—