diff --git a/regx/contain.go b/regx/contain.go new file mode 100644 index 0000000..79d6533 --- /dev/null +++ b/regx/contain.go @@ -0,0 +1,159 @@ +package regx + +import ( + "encoding/json" + "regexp" + "strconv" + "unicode" +) + +// ContainChinese 验证字符串是否包含中文字符 +func (rr *Regx) ContainChinese(s string) bool { + var containChineseRegexMatcher = regexp.MustCompile("[\u4e00-\u9fa5]") + return containChineseRegexMatcher.MatchString(s) +} + +// IsAlpha checks if the string contains only letters (a-zA-Z) +func (rr *Regx) IsAlpha(str string) bool { + var isAlphaRegexMatcher = regexp.MustCompile(`^[a-zA-Z]+$`) + return isAlphaRegexMatcher.MatchString(str) +} + +// IsAllUpper check if the string is all upper case letters A-Z +func (rr *Regx) IsAllUpper(str string) bool { + for _, r := range str { + if !unicode.IsUpper(r) { + return false + } + } + return str != "" +} + +// IsAllLower check if the string is all lower case letters a-z +func (rr *Regx) IsAllLower(str string) bool { + for _, r := range str { + if !unicode.IsLower(r) { + return false + } + } + return str != "" +} + +// ContainUpper check if the string contain at least one upper case letter A-Z +func (rr *Regx) ContainUpper(str string) bool { + for _, r := range str { + if unicode.IsUpper(r) && unicode.IsLetter(r) { + return true + } + } + return false +} + +// ContainLower check if the string contain at least one lower case letter A-Z +func (rr *Regx) ContainLower(str string) bool { + for _, r := range str { + if unicode.IsLower(r) && unicode.IsLetter(r) { + return true + } + } + return false +} + +// ContainLetter check if the string contain at least one letter +func (rr *Regx) ContainLetter(str string) bool { + var containLetterRegexMatcher = regexp.MustCompile(`[a-zA-Z]`) + return containLetterRegexMatcher.MatchString(str) +} + +// IsJSON checks if the string is valid JSON +func (rr *Regx) IsJSON(str string) bool { + var js json.RawMessage + return json.Unmarshal([]byte(str), &js) == nil +} + +// IsFloatStr check if the string can convert to a float. +func (rr *Regx) IsFloatStr(str string) bool { + _, e := strconv.ParseFloat(str, 64) + return e == nil +} + +// IsIntStr check if the string can convert to a integer. +func (rr *Regx) IsIntStr(str string) bool { + var isIntStrRegexMatcher = regexp.MustCompile(`^[\+-]?\d+$`) + return isIntStrRegexMatcher.MatchString(str) +} + +// IsNumberStr check if the string can convert to a number. +func (rr *Regx) IsNumberStr(s string) bool { + return rr.IsIntStr(s) || rr.IsFloatStr(s) +} + +// IsChineseIdNum check if the string is chinese id number. +func IsChineseIdNum(id string) bool { + var isChineseIdNumRegexMatcher *regexp.Regexp = regexp.MustCompile(`^[1-9]\d{5}(18|19|20|21|22)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$`) + return isChineseIdNumRegexMatcher.MatchString(id) +} + +// IsCreditCard check if the string is credit card. +func IsCreditCard(creditCart string) bool { + var isCreditCardRegexMatcher *regexp.Regexp = regexp.MustCompile(`^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|(222[1-9]|22[3-9][0-9]|2[3-6][0-9]{2}|27[01][0-9]|2720)[0-9]{12}|6(?:011|5[0-9][0-9])[0-9]{12}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\\d{3})\\d{11}|6[27][0-9]{14})$`) + return isCreditCardRegexMatcher.MatchString(creditCart) +} + +// IsBase64 check if the string is base64 string. +func IsBase64(base64 string) bool { + var isBase64RegexMatcher *regexp.Regexp = regexp.MustCompile(`^(?:[A-Za-z0-9+\\/]{4})*(?:[A-Za-z0-9+\\/]{2}==|[A-Za-z0-9+\\/]{3}=|[A-Za-z0-9+\\/]{4})$`) + return isBase64RegexMatcher.MatchString(base64) +} + +// IsEmptyString check if the string is empty. +func IsEmptyString(str string) bool { + return len(str) == 0 +} + +// IsRegexMatch check if the string match the regexp +func IsRegexMatch(str, regex string) bool { + reg := regexp.MustCompile(regex) + return reg.MatchString(str) +} + +// IsStrongPassword check if the string is strong password, if len(password) is less than the length param, return false +// Strong password: alpha(lower+upper) + number + special chars(!@#$%^&*()?><) +func IsStrongPassword(password string, length int) bool { + if len(password) < length { + return false + } + var num, lower, upper, special bool + for _, r := range password { + switch { + case unicode.IsDigit(r): + num = true + case unicode.IsUpper(r): + upper = true + case unicode.IsLower(r): + lower = true + case unicode.IsSymbol(r), unicode.IsPunct(r): + special = true + } + } + + return num && lower && upper && special +} + +// IsWeakPassword check if the string is weak password +// Weak password: only letter or only number or letter + number +func IsWeakPassword(password string) bool { + var num, letter, special bool + for _, r := range password { + switch { + case unicode.IsDigit(r): + num = true + case unicode.IsLetter(r): + letter = true + case unicode.IsSymbol(r), unicode.IsPunct(r): + special = true + } + } + + return (num || letter) && !special +} diff --git a/regx/contain_test.go b/regx/contain_test.go new file mode 100644 index 0000000..afa847a --- /dev/null +++ b/regx/contain_test.go @@ -0,0 +1,28 @@ +package regx + +import ( + "fmt" + "testing" +) + +func TestRegx_ContainChinese(t *testing.T) { + fmt.Println(regx.ContainChinese("中文")) + fmt.Println(regx.ContainChinese("abc")) + fmt.Println(regx.ContainChinese("中文abc")) +} + +func TestRegx_Contains(t *testing.T) { + fmt.Println(regx.IsAlpha("中文")) + fmt.Println(regx.IsAlpha("abc")) + fmt.Println(regx.IsAlpha("中文abc")) + fmt.Println(regx.IsAllUpper("中文")) + fmt.Println(regx.IsAllUpper("abc")) + fmt.Println(regx.IsAllUpper("中文abc")) +} + +func TestRegx_IsFloatStr(t *testing.T) { + fmt.Println(regx.IsFloatStr("1.1")) + fmt.Println(regx.IsFloatStr("1.1.1")) + fmt.Println(regx.IsFloatStr("3")) + fmt.Println(regx.IsJSON("{\"name\":\"中文\"}")) +} diff --git a/regx/ip.go b/regx/ip.go index 1f02ddb..204834e 100644 --- a/regx/ip.go +++ b/regx/ip.go @@ -2,17 +2,12 @@ package regx import ( "net" + "net/url" "regexp" + "strconv" + "strings" ) -func (rr *Regx) IsIp(address string) bool { - ip := net.ParseIP(address) - if ip != nil { - return true - } - return false - -} func (rr *Regx) IsIpRegx(address string) bool { ipReg := `^((0|[1-9]\d?|1\d\d|2[0-4]\d|25[0-5])\.){3}(0|[1-9]\d?|1\d\d|2[0-4]\d|25[0-5])$` r, _ := regexp.Compile(ipReg) @@ -22,3 +17,61 @@ func (rr *Regx) IsIpRegx(address string) bool { } return false } + +// IsIp check if the string is a ip address. +func (rr *Regx) IsIp(ipstr string) bool { + ip := net.ParseIP(ipstr) + return ip != nil +} + +// IsIpV4 check if the string is a ipv4 address. +func (rr *Regx) IsIpV4(ipstr string) bool { + ip := net.ParseIP(ipstr) + if ip == nil { + return false + } + return strings.Contains(ipstr, ".") +} + +// IsIpV6 check if the string is a ipv6 address. +func (rr *Regx) IsIpV6(ipstr string) bool { + ip := net.ParseIP(ipstr) + if ip == nil { + return false + } + return strings.Contains(ipstr, ":") +} + +// IsPort check if the string is a valid net port. +func (rr *Regx) IsPort(str string) bool { + if i, err := strconv.ParseInt(str, 10, 64); err == nil && i > 0 && i < 65536 { + return true + } + return false +} + +// IsUrl check if the string is url. +func (rr *Regx) IsUrl(str string) bool { + var isUrlRegexMatcher *regexp.Regexp = regexp.MustCompile(`^((ftp|http|https?):\/\/)?(\S+(:\S*)?@)?((([1-9]\d?|1\d\d|2[01]\d|22[0-3])(\.(1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.([0-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(([a-zA-Z0-9]+([-\.][a-zA-Z0-9]+)*)|((www\.)?))?(([a-z\x{00a1}-\x{ffff}0-9]+-?-?)*[a-z\x{00a1}-\x{ffff}0-9]+)(?:\.([a-z\x{00a1}-\x{ffff}]{2,}))?))(:(\d{1,5}))?((\/|\?|#)[^\s]*)?$`) + if str == "" || len(str) >= 2083 || len(str) <= 3 || strings.HasPrefix(str, ".") { + return false + } + u, err := url.Parse(str) + if err != nil { + return false + } + if strings.HasPrefix(u.Host, ".") { + return false + } + if u.Host == "" && (u.Path != "" && !strings.Contains(u.Path, ".")) { + return false + } + + return isUrlRegexMatcher.MatchString(str) +} + +// IsDns check if the string is dns. +func (rr *Regx) IsDns(dns string) bool { + var isDnsRegexMatcher *regexp.Regexp = regexp.MustCompile(`^[a-zA-Z]([a-zA-Z0-9\-]+[\.]?)*[a-zA-Z0-9]$`) + return isDnsRegexMatcher.MatchString(dns) +} diff --git a/regx/ip_test.go b/regx/ip_test.go index f74f2fd..f8ad616 100644 --- a/regx/ip_test.go +++ b/regx/ip_test.go @@ -14,3 +14,14 @@ func TestRegx_IsIp(t *testing.T) { fmt.Println(regx.IsIpRegx("114.114.114.314")) fmt.Println(regx.IsIpRegx("www.baidu.com")) } + +func TestRegx_IsIpV4(t *testing.T) { + fmt.Println(regx.IsIpV4("222.222.222.2242")) + fmt.Println(regx.IsIpV6("222.222.222.2242")) + +} +func TestRegx_IsDns(t *testing.T) { + fmt.Println(regx.IsDns("www.baidu.com")) + fmt.Println(regx.IsDns("www.baidu.com.cn")) + fmt.Println(regx.IsDns("www.baidu.com.cn.cn")) +} diff --git a/regx/mail.go b/regx/mail.go index bc93284..46d1349 100644 --- a/regx/mail.go +++ b/regx/mail.go @@ -8,3 +8,9 @@ func (rr *Regx) IsMail(mail string) bool { reg := regexp.MustCompile(pattern) return reg.MatchString(mail) } + +// IsEmail check if the string is a email address. +func (rr *Regx) IsEmail(email string) bool { + var isEmailRegexMatcher = regexp.MustCompile(`\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*`) + return isEmailRegexMatcher.MatchString(email) +} diff --git a/regx/mail_test.go b/regx/mail_test.go index fac79ea..a4c7fbb 100644 --- a/regx/mail_test.go +++ b/regx/mail_test.go @@ -12,3 +12,11 @@ func TestRegx_IsMail(t *testing.T) { fmt.Println(regx.IsMail("www@baidu.com.cn")) } + +func TestRegx_IsEmail(t *testing.T) { + fmt.Println(regx.IsEmail("www.baidu.com")) + fmt.Println(regx.IsEmail("www@baiducom")) + fmt.Println(regx.IsEmail("www@baidu.com")) + fmt.Println(regx.IsEmail("www@baidu.com.cn")) + +} diff --git a/regx/phone.go b/regx/phone.go index 58cb9d3..f1b0c65 100644 --- a/regx/phone.go +++ b/regx/phone.go @@ -8,3 +8,16 @@ func (rr *Regx) IsPhone(phone string) bool { reg := regexp.MustCompile(regular) return reg.MatchString(phone) } + +// IsChineseMobile check if the string is chinese mobile number. +func (rr *Regx) IsChineseMobile(mobileNum string) bool { + var isChineseMobileRegexMatcher = regexp.MustCompile("^((13[0-9])|(14[5,7])|(15[0-3,5-9])|(17[0,3,5-8])|(18[0-9])|166|198|199|(147))\\d{8}$") + return isChineseMobileRegexMatcher.MatchString(mobileNum) +} + +// IsChinesePhone check if the string is chinese phone number. +// Valid chinese phone is xxx-xxxxxxxx or xxxx-xxxxxxx +func (rr *Regx) IsChinesePhone(phone string) bool { + var isChinesePhoneRegexMatcher = regexp.MustCompile(`\d{3}-\d{8}|\d{4}-\d{7}`) + return isChinesePhoneRegexMatcher.MatchString(phone) +} diff --git a/regx/phone_test.go b/regx/phone_test.go index 33fd02c..2691749 100644 --- a/regx/phone_test.go +++ b/regx/phone_test.go @@ -10,3 +10,15 @@ func TestRegx_IsPhone(t *testing.T) { fmt.Println(regx.IsPhone("188888888888888")) fmt.Println(regx.IsPhone("28888888888")) } + +func TestRegx_IsChineseMobile(t *testing.T) { + fmt.Println(regx.IsChineseMobile("18888888888")) + fmt.Println(regx.IsChineseMobile("188888888888888")) + fmt.Println(regx.IsChineseMobile("28888888888")) +} + +func TestRegx_IsChinesePhone(t *testing.T) { + fmt.Println(regx.IsChinesePhone("622-888888888")) + fmt.Println(regx.IsChinesePhone("188888888888888")) + fmt.Println(regx.IsChinesePhone("28888888888")) +}