Standard Library Utilities
Regexp Match
The regexp package checks text against a pattern when simple string helpers are not enough.
Regexp Match
regexp_match.go
package main
import (
"fmt"
"regexp"
)
func main() {
var email =
pattern := regexp.MustCompile(`^[a-z]+@example\.(com|org)$`)
matched := pattern.MatchString(email)
label := "invalid"
if matched {
label = "example address"
}
fmt.Println("email=", email)
fmt.Println("matched=", matched)
fmt.Println("label=", label)
}
package main
import (
"fmt"
"regexp"
)
func main() {
var email =
pattern := regexp.MustCompile(`^[a-z]+@example\.(com|org)$`)
matched := pattern.MatchString(email)
label := "invalid"
if matched {
label = "example address"
}
fmt.Println("email=", email)
fmt.Println("matched=", matched)
fmt.Println("label=", label)
}
package main
import (
"fmt"
"regexp"
)
func main() {
var email =
pattern := regexp.MustCompile(`^[a-z]+@example\.(com|org)$`)
matched := pattern.MatchString(email)
label := "invalid"
if matched {
label = "example address"
}
fmt.Println("email=", email)
fmt.Println("matched=", matched)
fmt.Println("label=", label)
}
regular expression
A regular expression is a compact pattern for matching text.