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
Replace first occurrence of string using Regexp
Example: How to use ReadAtLeast from IO Package in Golang?
Various examples of Carbon date-time package in Golang
How to check if a string contains certain characters in Golang?
How to replace emoji characters in string using regex in Golang?
Find length of Channel, Pointer, Slice, String and Map