Skip to content

Commit

Permalink
feat: regex
Browse files Browse the repository at this point in the history
  • Loading branch information
leafney committed Nov 21, 2023
1 parent cd29352 commit 30d471d
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 15 deletions.
8 changes: 4 additions & 4 deletions mask.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}
Expand Down
52 changes: 44 additions & 8 deletions reg.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,69 @@ 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")
}
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)
}
8 changes: 5 additions & 3 deletions reg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 嘿"))
}

0 comments on commit 30d471d

Please sign in to comment.