Skip to content

Instantly share code, notes, and snippets.

@rickt
Created June 20, 2014 21:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rickt/c5d249e8e975a0b55882 to your computer and use it in GitHub Desktop.
Save rickt/c5d249e8e975a0b55882 to your computer and use it in GitHub Desktop.
package main
import "fmt"
import "regexp"
var email = regexp.MustCompile(`^[^@]+@[^@.]+\.[^@.]+$`)
var shortPhone = regexp.MustCompile(`^[0-9][0-9][0-9][.\-]?[0-9][0-9][0-9][0-9]$`)
var longPhone = regexp.MustCompile(`^[(]?[0-9][0-9][0-9][). \-]*[0-9][0-9][0-9][.\-]?[0-9][0-9][0-9][0-9]$`)
func main() {
contacts := []string{
"(111) 555-4444",
"foo@example.com",
"555-1234",
"123.555.4567",
"1 infinite loop",
}
for _, contact := range contacts {
switch {
case email.MatchString(contact):
fmt.Println(contact, "is an email")
case shortPhone.MatchString(contact):
fmt.Println(contact, "is a short phone number")
case longPhone.MatchString(contact):
fmt.Println(contact, "is a long phone number")
default:
fmt.Println(contact, "is not recognized")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment