Skip to content

Instantly share code, notes, and snippets.

@hfeeki
Forked from kylelemons/regexpswitch.go
Created April 3, 2013 16:55
Show Gist options
  • Save hfeeki/5303048 to your computer and use it in GitHub Desktop.
Save hfeeki/5303048 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