This repository has been archived by the owner on May 26, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
regex.go
130 lines (115 loc) · 4.24 KB
/
regex.go
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Copyright 2013 com authors
//
// Licensed under the Apache License, Version 2.0 (the "License"): you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations
// under the License.
package regex
import (
"regexp"
"strings"
)
const (
// email
patternEmail = `(?i)[A-Z0-9._%+-]+@(?:[A-Z0-9-]+\.)+[A-Z]{2,6}`
patternStrictEmail = `(?i)[A-Z0-9!#$%&'*+/=?^_{|}~-]+` +
`(?:\.[A-Z0-9!#$%&'*+/=?^_{|}~-]+)*` +
`@(?:[A-Z0-9](?:[A-Z0-9-]*[A-Z0-9])?\.)+` +
`[A-Z0-9](?:[A-Z0-9-]*[A-Z0-9])?`
// url
patternURL = `(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@\-\/]))?`
// RGB颜色
patternRGBColor = "^rgb\\(\\s*(0|[1-9]\\d?|1\\d\\d?|2[0-4]\\d|25[0-5])" +
"\\s*,\\s*(0|[1-9]\\d?|1\\d\\d?|2[0-4]\\d|25[0-5])\\s*,\\s*(0|[1-9]" +
"\\d?|1\\d\\d?|2[0-4]\\d|25[0-5])\\s*\\)$"
// 十六进制颜色
patternRegexHexColor = `^#?([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$`
// 字母或数字
patternLetterNumeric = `^[a-zA-Z0-9]+$`
// 全中文
patternChinese = "^[\u4e00-\u9fa5]+$"
// 中文名
patternChineseName = "^[\u4e00-\u9fa5][.•·\u4e00-\u9fa5]{0,30}[\u4e00-\u9fa5]$"
// 词语,不以下划线开头的中文、英文、数字、下划线、空格
patternWord = "^[a-zA-Z0-9\u4e00-\u9fa5][a-zA-Z0-9_\u4e00-\u9fa5]+$"
// 全空白字符
patternWhitespaceAll = "^[[:space:]]+$"
// 含有空白字符
patternWhitespaceHas = ".*[[:space:]]"
)
// 全局预编译regex
var (
RegexEmail = regexp.MustCompile(patternEmail)
RegexStrictEmail = regexp.MustCompile(patternStrictEmail)
RegexURL = regexp.MustCompile(patternURL)
RegexRGBColor = regexp.MustCompile(patternRGBColor)
RegexHexColor = regexp.MustCompile(patternRegexHexColor)
RegexAlphaNumeric = regexp.MustCompile(patternLetterNumeric)
RegexChinese = regexp.MustCompile(patternChinese)
RegexChineseName = regexp.MustCompile(patternChineseName)
RegexWord = regexp.MustCompile(patternWord)
RegexWhitespaceAll = regexp.MustCompile(patternWhitespaceAll)
RegexWhitespaceHas = regexp.MustCompile(patternWhitespaceHas)
)
// IsEmail validates string is an email address, if not return false
// basically validation can match 99% cases
func IsEmail(s string) bool {
return s != "" && RegexEmail.MatchString(s)
}
// IsEmailRFC validates string is an email address, if not return false
// this validation omits RFC 2822
func IsEmailRFC(email string) bool {
return email != "" && RegexStrictEmail.MatchString(email)
}
// IsURL validates string is a url link, if not return false
// simple validation can match 99% cases
func IsURL(s string) bool {
return s != "" && RegexURL.MatchString(s)
}
// IsRGBColor 是否是rgb颜色格式
// rgb(0,31, 255)
func IsRGBColor(s string) bool {
return s != "" && RegexRGBColor.MatchString(s)
}
// IsHexColor 检查是否十六进制颜色,并返回带"#"的修正值.
func IsHexColor(s string) (string, bool) {
ok := s != "" && RegexHexColor.MatchString(s)
if ok {
s = strings.ToUpper(s)
if !strings.ContainsRune(s, '#') {
s = "#" + s
}
}
return s, ok
}
// IsAlphaNumeric 是否字母或数字.
func IsAlphaNumeric(s string) bool {
return s != "" && RegexAlphaNumeric.MatchString(s)
}
// IsWord 是否词语(不以下划线开头的中文、英文、数字、下划线、空格).
func IsWord(str string) bool {
return str != "" && RegexWord.MatchString(str)
}
// IsChinese 字符串是否全部中文.
func IsChinese(str string) bool {
return str != "" && RegexChinese.MatchString(str)
}
// IsChineseName 字符串是否中文名.
func IsChineseName(str string) bool {
return str != "" && RegexChineseName.MatchString(str)
}
// IsWhitespaces 是否全部空白字符,不包括空字符串.
func IsWhitespaces(str string) bool {
return str != "" && RegexWhitespaceAll.MatchString(str)
}
// HasWhitespace 是否含有空白字符
func HasWhitespace(str string) bool {
return str != "" && RegexWhitespaceHas.MatchString(str)
}