GolangNote

Golang笔记

GoLang 正则判断字符是不是中文

Permalink

GoLang 判断字符是不是中文

Go: 判断字符是不是中文
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package main

import (
    "fmt"
    "regexp"
    "unicode"
)

func main() {
    str := "中文文文文"
    var hzRegexp = regexp.MustCompile("^[\u4e00-\u9fa5]{3,8}$")
    fmt.Println(hzRegexp.MatchString(str))
    fmt.Println(IsChineseChar(str))
}

func IsChineseChar(str string) bool {
    for _, r := range str {
        if unicode.Is(unicode.Scripts["Han"], r) {
            return true
        }
    }
    return false
}

另外一些常用的字符检测正则:

Go: check 常用的字符检测正则
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package check

import (
    "regexp"
)

const (
    // 中国大陆手机号码正则匹配, 不是那么太精细
    // 只要是 13,14,15,17,18 开头的 11 位数字就认为是中国手机号
    chinaMobilePattern = `^1[34578][0-9]{9}$`
    // 用户昵称的正则匹配, 合法的字符有 0-9, A-Z, a-z, _, 汉字
    // 字符 '_' 只能出现在中间且不能重复, 如 "__"
    nicknamePattern = `^[a-z0-9A-Z\p{Han}]+(_[a-z0-9A-Z\p{Han}]+)*$`
    // 用户名的正则匹配, 合法的字符有 0-9, A-Z, a-z, _
    // 第一个字母不能为 _, 0-9
    // 最后一个字母不能为 _, 且 _ 不能连续
    usernamePattern = `^[a-zA-Z][a-z0-9A-Z]*(_[a-z0-9A-Z]+)*$`
    // 电子邮箱的正则匹配, 考虑到各个网站的 mail 要求不一样, 这里匹配比较宽松
    // 邮箱用户名可以包含 0-9, A-Z, a-z, -, _, .
    // 开头字母不能是 -, _, .
    // 结尾字母不能是 -, _, .
    // -, _, . 这三个连接字母任意两个不能连续, 如不能出现 --, __, .., -_, -., _.
    // 邮箱的域名可以包含 0-9, A-Z, a-z, -
    // 连接字符 - 只能出现在中间, 不能连续, 如不能 --
    // 支持多级域名, x@y.z, x@y.z.w, x@x.y.z.w.e
    mailPattern = `^[a-z0-9A-Z]+([\-_\.][a-z0-9A-Z]+)*@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)*\.)+[a-zA-Z]{2,4}$`
)

var (
    chinaMobileRegexp = regexp.MustCompile(chinaMobilePattern)
    nicknameRegexp    = regexp.MustCompile(nicknamePattern)
    usernameRegexp    = regexp.MustCompile(usernamePattern)
    mailRegexp        = regexp.MustCompile(mailPattern)
)

// 检验是否为合法的中国手机号, 不是那么太精细
// 只要是 13,14,15,18 开头的 11 位数字就认为是中国手机号
func IsChinaMobile(b []byte) bool {
    if len(b) != 11 {
        return false
    }
    return chinaMobileRegexp.Match(b)
}

// 同 func IsChinaMobile(b []byte) bool
func IsChinaMobileString(str string) bool {
    if len(str) != 11 {
        return false
    }
    return chinaMobileRegexp.MatchString(str)
}

// 检验是否为合法的昵称, 合法的字符有 0-9, A-Z, a-z, _, 汉字
// 字符 '_' 只能出现在中间且不能重复, 如 "__"
func IsNickname(b []byte) bool {
    if len(b) == 0 {
        return false
    }
    return nicknameRegexp.Match(b)
}

// 同 func IsNickname(b []byte) bool
func IsNicknameString(str string) bool {
    if len(str) == 0 {
        return false
    }
    return nicknameRegexp.MatchString(str)
}

// 检验是否为合法的用户名, 合法的字符有 0-9, A-Z, a-z, _
// 第一个字母不能为 _, 0-9
// 最后一个字母不能为 _, 且 _ 不能连续
func IsUserName(b []byte) bool {
    if len(b) == 0 {
        return false
    }
    return usernameRegexp.Match(b)
}

// 同 func IsName(b []byte) bool
func IsUserNameString(str string) bool {
    if len(str) == 0 {
        return false
    }
    return usernameRegexp.MatchString(str)
}

// 检验是否为合法的电子邮箱, 考虑到各个网站的 mail 要求不一样, 这里匹配比较宽松
// 邮箱用户名可以包含 0-9, A-Z, a-z, -, _, .
// 开头字母不能是 -, _, .
// 结尾字母不能是 -, _, .
// -, _, . 这三个连接字母任意两个不能连续, 如不能出现 --, __, .., -_, -., _.
// 邮箱的域名可以包含 0-9, A-Z, a-z, -
// 连接字符 - 只能出现在中间, 不能连续, 如不能 --
// 支持多级域名, x@y.z, x@y.z.w, x@x.y.z.w.e
func IsMail(b []byte) bool {
    if len(b) < 6 { // x@x.xx
        return false
    }
    return mailRegexp.Match(b)
}

// 同 func IsMail(b []byte) bool
func IsMailString(str string) bool {
    if len(str) < 6 { // x@x.xx
        return false
    }
    return mailRegexp.MatchString(str)
}

本文网址: https://golangnote.com/topic/90.html 转摘请注明来源

Related articles

Golang 泛型性能初识

编程时,我们通常需要编写“泛型”函数,其中确切的数据类型并不重要。例如,您可能想编写一个简单的函数来对数字进行求和。Go 直到最近才有这个概念,但最近才添加了它(从1.18版开始)。...

Golang quicktemplate 模版快速入门

Golang 有很多的模版引擎,自带的 `html/template` 也很好,大多数情况都能满足需求,只是有些逻辑、条件判断不好在模版里实现, `quicktemplate` 是个很好的选择。...

Golang telegram 机器人小试

telegram 的机器人接口很开放,使用简单,100%开放无限制,相对微信服务号、公众号好很多。用来做一些小应用也很方便。下面是使用golang sdk 开发telegram 机器人的经验。...

Golang 时区时差处理方式

个人习惯用 0 时区时间戳记录时间,可以方便转到不同时区,下面介绍 Golang 时区时差处理...

Write a Comment to "GoLang 正则判断字符是不是中文"

Submit Comment Login
Based on Golang + fastHTTP + sdb | go1.20 Processed in 12ms