Email Domain Part (After @) in GO
Validate just the domain portion of an email address — the bit after the @.
Try it in the GO tester →Pattern
regexGO
^[a-zA-Z0-9](?:[a-zA-Z0-9.\-]{0,253}[a-zA-Z0-9])?\.[a-zA-Z]{2,24}$Go (RE2) code
goGo
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`^[a-zA-Z0-9](?:[a-zA-Z0-9.\-]{0,253}[a-zA-Z0-9])?\.[a-zA-Z]{2,24}$`)
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
Starts and ends with alphanumeric per RFC. Allows dots and hyphens in the middle. Length capped at 255 chars total. Trailing \.[a-zA-Z]{2,24}$ requires a TLD of 2–24 characters (covers everything from `.io` to `.international`).
Examples
Input
example.comMatches
example.com
Input
mail.sub.domain.orgMatches
mail.sub.domain.org
Input
no-tldNo match
—