Skip to content

Instantly share code, notes, and snippets.

@cwbak
Created July 22, 2019 12:06
Show Gist options
  • Save cwbak/56b32f91713a6b013aa9c5d2de410129 to your computer and use it in GitHub Desktop.
Save cwbak/56b32f91713a6b013aa9c5d2de410129 to your computer and use it in GitHub Desktop.
// http://www.golangprograms.com/regular-expression-to-validate-email-address.html
//Regular expression to validate email address
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))
}
// http://www.golangprograms.com/regular-expression-to-validate-phone-number.html
// Regular expression to validate phone number
package main
import (
"fmt"
"regexp"
)
func main() {
str1 := "1(234)5678901x1234"
str2 := "(+351) 282 43 50 50"
str3 := "90191919908"
str4 := "555-8909"
str5 := "001 6867684"
str6 := "001 6867684x1"
str7 := "1 (234) 567-8901"
str8 := "1-234-567-8901 ext1234"
re := regexp.MustCompile(`^(?:(?:\(?(?:00|\+)([1-4]\d\d|[1-9]\d?)\)?)?[\-\.\ \\\/]?)?((?:\(?\d{1,}\)?[\-\.\ \\\/]?){0,})(?:[\-\.\ \\\/]?(?:#|ext\.?|extension|x)[\-\.\ \\\/]?(\d+))?$`)
fmt.Printf("Pattern: %v\n", re.String()) // print pattern
fmt.Printf("\nPhone: %v\t:%v\n", str1, re.MatchString(str1))
fmt.Printf("Phone: %v\t:%v\n", str2, re.MatchString(str2))
fmt.Printf("Phone: %v\t\t:%v\n", str3, re.MatchString(str3))
fmt.Printf("Phone: %v\t\t\t:%v\n", str4, re.MatchString(str4))
fmt.Printf("Phone: %v\t\t:%v\n", str5, re.MatchString(str5))
fmt.Printf("Phone: %v\t\t:%v\n", str6, re.MatchString(str6))
fmt.Printf("Phone: %v\t\t:%v\n", str7, re.MatchString(str7))
fmt.Printf("Phone: %v\t:%v\n", str8, re.MatchString(str8))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment