micro-bundle/pkg/utils/str.go

21 lines
589 B
Go
Raw Normal View History

2025-06-09 05:58:27 +00:00
package utils
import "regexp"
func IsPhoneNumber(phone string) bool {
// 正则表达式匹配常见的电话号码格式
// 1. 11位手机号13x, 14x, 15x, 16x, 17x, 18x, 19x开头
// 2. 3-4位区号+7-8位号码可含-或空格分隔)
// 3. 国际号码(+开头)
pattern := `^(?:\+?[0-9]{1,4}[- ]?)?` + // 国际前缀
`(?:\([0-9]{1,4}\)[- ]?)?` + // 可能有括号的区号
`(?:[0-9]{7,15}|` + // 7-15位数字或
`1[3-9][0-9]{9})$` // 11位手机号
matched, err := regexp.MatchString(pattern, phone)
if err != nil {
return false
}
return matched
}