diff --git a/mask.go b/mask.go index 52b7494..9d87096 100644 --- a/mask.go +++ b/mask.go @@ -5,12 +5,12 @@ import ( "strings" ) -// MaskPhone 手机号 前3后4 +// MaskPhone 手机号 保留前3后4 func MaskPhone(phone string) string { return MaskStr(phone, 3, 4, "*", 4) } -// MaskEmail 邮箱 仅用户名部分 前1后1 +// MaskEmail 邮箱 仅用户名部分 保留前1后1 func MaskEmail(email string) string { if !strings.Contains(email, "@") { return email @@ -27,12 +27,12 @@ func MaskPwd(pwd string) string { return "********" } -// MaskBankCard 银行卡号 前4后4 +// MaskBankCard 银行卡号 保留前4后4 func MaskBankCard(card string) string { return MaskStr(card, 4, 4, "*", 4) } -// MaskIDCard 身份证号 前3后4 +// MaskIDCard 身份证号 保留前3后4 func MaskIDCard(id string) string { return MaskStr(id, 3, 4, "*", 4) } diff --git a/reg.go b/reg.go index ada3170..e024a3c 100644 --- a/reg.go +++ b/reg.go @@ -3,22 +3,22 @@ package rose import "regexp" // IsPhone 正则匹配手机号格式是否正确(相对宽松的校验格式 -func IsPhone(phone string) bool { +func RegIsPhone(phone string) bool { pattern := `^1[3-9](\d{9})$` re := regexp.MustCompile(pattern) return re.MatchString(phone) } // IsEmail 正则匹配邮箱格式是否正确 -func IsEmail(email string) bool { +func RegIsEmail(email string) bool { pattern := `^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$` re := regexp.MustCompile(pattern) return re.MatchString(email) } -// PhoneNumStar 隐藏手机号码中间4位 -func PhoneNumStar(phone string) string { - if IsPhone(phone) { +// PhoneNumMask 手机号码中间4位掩码处理 +func PhoneNumMask(phone string) string { + if RegIsPhone(phone) { pattern := `^(\d{3})\d{4}(\d{4})$` re := regexp.MustCompile(pattern) return re.ReplaceAllString(phone, "$1****$2") @@ -26,10 +26,46 @@ func PhoneNumStar(phone string) string { return phone } -// PhoneTailNum 获取手机尾号后4位 -func PhoneTailNum(phone string) string { - if IsPhone(phone) { +// PhoneNumTail 获取手机尾号后4位 +func PhoneNumTail(phone string) string { + if RegIsPhone(phone) { return phone[len(phone)-4:] } return phone } + +// RegIsNormalString 校验字符串是否只包含字母、数字和下划线 +func RegIsNormalString(input string) bool { + // 定义正则表达式,只允许字母、数字和下划线 + reg := regexp.MustCompile(`^[A-Za-z0-9_]+$`) + + // 校验字符串是否符合正则表达式 + if !reg.MatchString(input) { + return false + } + + // 校验字符串是否包含空格 + if RegIsContainsSpace(input) { + return false + } + + return true +} + +// RegIsContainsSpace 判断字符串是否包含空格 +func RegIsContainsSpace(input string) bool { + // 定义正则表达式,匹配空格 + reg := regexp.MustCompile(`\s`) + + // 校验字符串是否包含空格 + return reg.MatchString(input) +} + +// RegIsChinese 校验字符串是否只包含中文字符 +func RegIsChinese(input string) bool { + // 定义正则表达式,匹配中文字符 + reg := regexp.MustCompile(`^[\p{Han}]+$`) + + // 校验字符串是否只包含中文字符 + return reg.MatchString(input) +} diff --git a/reg_test.go b/reg_test.go index de98b99..ee28906 100644 --- a/reg_test.go +++ b/reg_test.go @@ -12,7 +12,9 @@ import "testing" func TestPhoneNumStar(t *testing.T) { phone := "17092021576" - t.Log(IsPhone(phone)) - t.Log(PhoneNumStar(phone)) - t.Log(PhoneTailNum(phone)) + t.Log(RegIsPhone(phone)) + t.Log(PhoneNumMask(phone)) + t.Log(PhoneNumTail(phone)) + t.Log(RegIsNormalString("hello@qq")) + t.Log(RegIsChinese("你好"), RegIsChinese("he 嘿")) }