mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-13 01:02:28 +08:00
feat: add IsAlphaNumeric
This commit is contained in:
@@ -21,6 +21,7 @@ import (
|
|||||||
var (
|
var (
|
||||||
alphaMatcher *regexp.Regexp = regexp.MustCompile(`^[a-zA-Z]+$`)
|
alphaMatcher *regexp.Regexp = regexp.MustCompile(`^[a-zA-Z]+$`)
|
||||||
letterRegexMatcher *regexp.Regexp = regexp.MustCompile(`[a-zA-Z]`)
|
letterRegexMatcher *regexp.Regexp = regexp.MustCompile(`[a-zA-Z]`)
|
||||||
|
alphaNumericMatcher *regexp.Regexp = regexp.MustCompile(`^[a-zA-Z0-9-]+$`)
|
||||||
numberRegexMatcher *regexp.Regexp = regexp.MustCompile(`\d`)
|
numberRegexMatcher *regexp.Regexp = regexp.MustCompile(`\d`)
|
||||||
intStrMatcher *regexp.Regexp = regexp.MustCompile(`^[\+-]?\d+$`)
|
intStrMatcher *regexp.Regexp = regexp.MustCompile(`^[\+-]?\d+$`)
|
||||||
urlMatcher *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]*)?$`)
|
urlMatcher *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]*)?$`)
|
||||||
@@ -181,6 +182,12 @@ func IsJSON(str string) bool {
|
|||||||
return json.Unmarshal([]byte(str), &js) == nil
|
return json.Unmarshal([]byte(str), &js) == nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// IsAlphaNumericStr check if the string is alphanumeric.
|
||||||
|
// Play: todo
|
||||||
|
func IsAlphaNumeric(s string) bool {
|
||||||
|
return alphaNumericMatcher.MatchString(s)
|
||||||
|
}
|
||||||
|
|
||||||
// IsNumberStr check if the string can convert to a number.
|
// IsNumberStr check if the string can convert to a number.
|
||||||
// Play: https://go.dev/play/p/LzaKocSV79u
|
// Play: https://go.dev/play/p/LzaKocSV79u
|
||||||
func IsNumberStr(s string) bool {
|
func IsNumberStr(s string) bool {
|
||||||
|
|||||||
@@ -659,3 +659,21 @@ func TestIsChinaUnionPay(t *testing.T) {
|
|||||||
assert.Equal(true, IsChinaUnionPay("6250941006528599"))
|
assert.Equal(true, IsChinaUnionPay("6250941006528599"))
|
||||||
assert.Equal(false, IsChinaUnionPay("3782822463100007"))
|
assert.Equal(false, IsChinaUnionPay("3782822463100007"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestIsAlphaNumeric(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
assert := internal.NewAssert(t, "TestIsAlphaNumeric")
|
||||||
|
|
||||||
|
assert.Equal(true, IsAlphaNumeric("ABC"))
|
||||||
|
assert.Equal(true, IsAlphaNumeric("abc"))
|
||||||
|
assert.Equal(true, IsAlphaNumeric("aBC"))
|
||||||
|
assert.Equal(true, IsAlphaNumeric("1BC"))
|
||||||
|
assert.Equal(true, IsAlphaNumeric("1bc"))
|
||||||
|
assert.Equal(true, IsAlphaNumeric("123"))
|
||||||
|
|
||||||
|
assert.Equal(false, IsAlphaNumeric(""))
|
||||||
|
assert.Equal(false, IsAlphaNumeric("你好"))
|
||||||
|
assert.Equal(false, IsAlphaNumeric("A&"))
|
||||||
|
assert.Equal(false, IsAlphaNumeric("&@#$%^&*"))
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user