1.验证是否匹配正则;
package lib
import (
"regexp"
)
func IsEmail(email string) bool {
if email != "" {
if isOk, _ := regexp.MatchString("^[_a-z0-9-]+(\\.[_a-z0-9-]+)*@[a-z0-9-]+(\\.[a-z0-9-]+)*(\\.[a-z]{2,4})$", email); isOk {
return true
}
}
return false
}
func IsPhone(phoneStr string) bool {
if phoneStr != "" {
if isOk, _ := regexp.MatchString(`^\([\d]{3}\) [\d]{3}-[\d]{4}$`, phoneStr); isOk {
return isOk
}
}
return false
}
2.用正则抽取信息
re :=regexp.MustCompile("[\u0020-\u002F]|[\u003A-\u0040]|[\u005B-\u0060]|[\u00A0-\u00BF]")
re.MatchString(str)