Regular expression to validate email address
Example
package main
import (
"fmt"
"regexp"
)
func main() {
str1 := "ç$€§/az@gmail.com"
str2 := "abcd@gmail_yahoo.com"
str3 := "abcd@gmail-yahoo.com"
str4 := "abcd@gmailyahoo"
str5 := "abcd@gmail.yahoo"
re := regexp.MustCompile("^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$")
fmt.Printf("Pattern: %v\n", re.String()) // print pattern
fmt.Printf("\nEmail: %v :%v\n", str1, re.MatchString(str1))
fmt.Printf("Email: %v :%v\n", str2, re.MatchString(str2))
fmt.Printf("Email: %v :%v\n", str3, re.MatchString(str3))
fmt.Printf("Email: %v :%v\n", str4, re.MatchString(str4))
fmt.Printf("Email: %v :%v\n", str5, re.MatchString(str5))
}
Output
Pattern: ^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$
Email: ç$?§/az@gmail.com :false
Email: abcd@gmail_yahoo.com :false
Email: abcd@gmail-yahoo.com :true
Email: abcd@gmailyahoo :true
Email: abcd@gmail.yahoo :true
Most Helpful This Week
Example: Stack and Caller from RUNTIME package
Example of Pointers with Struct
How to create Map using the make function in Go?
How to verify a string only contains letters, numbers, underscores, and dashes in Golang?
Regular Expression to get a string between parentheses in Golang
How to append text to a file in Golang?
Catch values from Goroutines
Simple example of Map initialization in Go
What is GOPATH?
How to get Dimensions of an image type jpg jpeg png or gif ?