Email Local Part (Before @) in GO
Validate just the local part of an email address (the bit before the @).
Try it in the GO tester →Pattern
regexGO
^[a-zA-Z0-9._%+\-]{1,64}$Go (RE2) code
goGo
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`^[a-zA-Z0-9._%+\-]{1,64}$`)
input := `john.doe`
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
^ and $ anchor to the full string. [a-zA-Z0-9._%+\-] is the conservative local-part character set (dots, underscores, percent, plus, hyphen for sub-addressing). {1,64} enforces the RFC max length for the local part.
Examples
Input
john.doeMatches
john.doe
Input
user+tagMatches
user+tag
Input
has spaceNo match
—