Files
bl/common/utils/go-sensitive-word-1.3.3/tool.go
昔念 685069fded feat(cool): 添加敏感词过滤功能
- 引入 go-sensitive-word 敏感词过滤库
- 在全局初始化中加载敏感词库并配置过滤器
- 在创建玩家时应用敏感词过滤,替换不合适的昵称内容
2025-09-09 01:11:10 +08:00

65 lines
1.9 KiB
Go

package go_sensitive_word
import "regexp"
// HasEmail 判断字符串中是否存在邮箱地址
func HasEmail(s string) bool {
emailRegex := regexp.MustCompile(`[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}`)
return emailRegex.MatchString(s)
}
// MaskEmail 将字符串中存在的邮箱地址替换成 "*"
func MaskEmail(s string) string {
emailRegex := regexp.MustCompile(`[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}`)
masked := emailRegex.ReplaceAllStringFunc(s, func(email string) string {
return "***"
})
return masked
}
// HasURL 判断字符串中是否存在网址
func HasURL(s string) bool {
urlRegex := regexp.MustCompile(`https?://[^\s]+`)
return urlRegex.MatchString(s)
}
// MaskURL 将字符串中存在的网址替换成 "*"
func MaskURL(s string) string {
urlRegex := regexp.MustCompile(`https?://[^\s]+`)
masked := urlRegex.ReplaceAllStringFunc(s, func(url string) string {
return "***"
})
return masked
}
// HasDigit 判断字符串中是否存在指定个数的数字(大于等于该数字)
func HasDigit(s string, count int) bool {
digitRegex := regexp.MustCompile(`\d`)
matches := digitRegex.FindAllString(s, -1)
return len(matches) >= count
}
// MaskDigit 将字符串中存在的数字替换成 "*"
func MaskDigit(s string) string {
digitRegex := regexp.MustCompile(`\d`)
masked := digitRegex.ReplaceAllStringFunc(s, func(digit string) string {
return "*"
})
return masked
}
// HasWechatID 判断字符串中是否存在微信号
func HasWechatID(s string) bool {
wechatRegex := regexp.MustCompile(`[a-zA-Z][a-zA-Z0-9_-]{5,19}`)
return wechatRegex.MatchString(s)
}
// MaskWechatID 将字符串中存在的微信号替换成 "*"
func MaskWechatID(s string) string {
wechatRegex := regexp.MustCompile(`[a-zA-Z][a-zA-Z0-9_-]{5,19}`)
masked := wechatRegex.ReplaceAllStringFunc(s, func(wechatID string) string {
return "***"
})
return masked
}