Hostname (Single Label, RFC 1123) in PY
Validate a single-label hostname per RFC 1123: 1–63 chars, letters/digits/hyphens, can't start or end with hyphen.
Try it in the PY tester →Pattern
regexPY
^[a-zA-Z0-9](?:[a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?$Python (re) code
pyPython
import re
pattern = re.compile(r"^[a-zA-Z0-9](?:[a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?$")
input_text = "web-01"
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
^[a-zA-Z0-9] requires the first character to be a letter or digit. (?:[a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])? optionally matches up to 61 more characters (letters, digits, hyphens) followed by a letter/digit ending — preventing trailing hyphens. Combined max length: 63 characters per RFC 1123.
Examples
Input
web-01Matches
web-01
Input
host123Matches
host123
Input
-bad-startNo match
—