Domain Name in GO
Match fully-qualified domain names like example.com or api.sub.example.co.uk.
Try it in the GO tester →Pattern
regexGO
(?:[a-zA-Z0-9](?:[a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,} (flags: g)Go (RE2) code
goGo
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`(?:[a-zA-Z0-9](?:[a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}`)
input := `example.com`
for _, match := range re.FindAllString(input, -1) {
fmt.Println(match)
}
}Uses `regexp.MustCompile` (panics on bad patterns at startup) and `FindAllString` for all matches.
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
—