Regular Expressions 101

Community Patterns

DNS hostname well-formedness validation

4

Regular Expression
PCRE (PHP <7.3)

/
^(?:_?(?>[a-z\d][a-z\d-]{0,61}[a-z\d]|[a-z\d])\.)*(?:_?(?>[a-z\d][a-z\d-]{0,61}[a-z\d]|[a-z\d]))\.?$
/
i

Description

Validates that a DNS hostname is well-formed only. You will still need to check that the overall length of the hostname, including the implied trailing "." (if not present), does not exceed 255 bytes.

Submitted by Jeff Walter - 8 years ago