Regular Expressions 101

Community Patterns

Domain validation regex suitable for user input

0

Regular Expression
Golang

`
^(?:(?:[_a-z0-9](?:[_a-z0-9-]{0,61}[a-z0-9])?|[0-9]{1,3}?/[0-9]{2})\.)+(?:[a-z](?:[a-z0-9-]{0,61}[a-z0-9])?)?$
`
gim

Description

This regexp can be used to validate domain names in Golang. While it cannot enforce the 253 character limit (with optional trailing period not included) that can be easily done with a len(domain) <= 253 check.

This can be used as-is in other languages, even with RE2 regex engine. Non-capturing groups are used.

Submitted by anonymous - 4 years ago