regutils.go 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package regutils
  15. import (
  16. "net"
  17. "regexp"
  18. "strings"
  19. )
  20. var FUNCTION_REG *regexp.Regexp
  21. var UUID_REG *regexp.Regexp
  22. var UUID_EXACT_REG *regexp.Regexp
  23. var INTEGER_REG *regexp.Regexp
  24. var FLOAT_REG *regexp.Regexp
  25. var MACADDR_REG *regexp.Regexp
  26. var COMPACT_MACADDR_REG *regexp.Regexp
  27. var NSPTR_REG *regexp.Regexp
  28. var NAME_REG *regexp.Regexp
  29. var DOMAINNAME_REG *regexp.Regexp
  30. var DOMAINSRV_REG *regexp.Regexp
  31. var SIZE_REG *regexp.Regexp
  32. var MONTH_REG *regexp.Regexp
  33. var DATE_REG *regexp.Regexp
  34. var DATE_COMPACT_REG *regexp.Regexp
  35. var DATE_EXCEL_REG *regexp.Regexp
  36. var ISO_TIME_REG *regexp.Regexp
  37. var ISO_NO_SECOND_TIME_REG *regexp.Regexp
  38. var FULLISO_TIME_REG *regexp.Regexp
  39. var ISO_TIME_REG2 *regexp.Regexp
  40. var ISO_NO_SECOND_TIME_REG2 *regexp.Regexp
  41. var FULLISO_TIME_REG2 *regexp.Regexp
  42. var FULLISO_TIME_REG3 *regexp.Regexp
  43. var ZSTACK_TIME_REG *regexp.Regexp
  44. var COMPACT_TIME_REG *regexp.Regexp
  45. var MYSQL_TIME_REG *regexp.Regexp
  46. var CLICKHOUSE_TIME_REG *regexp.Regexp
  47. var NORMAL_TIME_REG *regexp.Regexp
  48. var FULLNORMAL_TIME_REG *regexp.Regexp
  49. var RFC2882_TIME_REG *regexp.Regexp
  50. var CEPH_TIME_REG *regexp.Regexp
  51. var EMAIL_REG *regexp.Regexp
  52. var CHINA_MOBILE_REG *regexp.Regexp
  53. var FS_FORMAT_REG *regexp.Regexp
  54. var US_CURRENCY_REG *regexp.Regexp
  55. var EU_CURRENCY_REG *regexp.Regexp
  56. func init() {
  57. FUNCTION_REG = regexp.MustCompile(`^\w+\(.*\)$`)
  58. UUID_REG = regexp.MustCompile(`[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}`)
  59. UUID_EXACT_REG = regexp.MustCompile(`^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$`)
  60. INTEGER_REG = regexp.MustCompile(`^[0-9]+$`)
  61. FLOAT_REG = regexp.MustCompile(`^\d+(\.\d*)?$`)
  62. MACADDR_REG = regexp.MustCompile(`^([0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}$`)
  63. COMPACT_MACADDR_REG = regexp.MustCompile(`^[0-9a-fA-F]{12}$`)
  64. NSPTR_REG = regexp.MustCompile(`^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\.in-addr\.arpa$`)
  65. NAME_REG = regexp.MustCompile(`^[a-zA-Z][a-zA-Z0-9._@-]*$`)
  66. DOMAINNAME_REG = regexp.MustCompile(`^([a-zA-Z0-9_]{1}[a-zA-Z0-9_-]{0,62}){1}(\.[a-zA-Z0-9_]{1}[a-zA-Z0-9_-]{0,62})*[\._]?$`)
  67. SIZE_REG = regexp.MustCompile(`^\d+[bBkKmMgG]?$`)
  68. MONTH_REG = regexp.MustCompile(`^\d{4}-\d{2}$`)
  69. DATE_REG = regexp.MustCompile(`^\d{4}-\d{2}-\d{2}$`)
  70. DATE_COMPACT_REG = regexp.MustCompile(`^\d{8}$`)
  71. DATE_EXCEL_REG = regexp.MustCompile(`^\d{2}-\d{2}-\d{2}$`)
  72. ISO_TIME_REG = regexp.MustCompile(`^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(Z|[+-]\d{2}:\d{2})$`)
  73. ISO_NO_SECOND_TIME_REG = regexp.MustCompile(`^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}(Z|[+-]\d{2}:\d{2})$`)
  74. FULLISO_TIME_REG = regexp.MustCompile(`^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3,9}(Z|[+-]\d{2}:\d{2})$`)
  75. ISO_TIME_REG2 = regexp.MustCompile(`^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}(Z|[+-]\d{2}:\d{2})$`)
  76. ISO_NO_SECOND_TIME_REG2 = regexp.MustCompile(`^\d{4}-\d{2}-\d{2} \d{2}:\d{2}(Z|[+-]\d{2}:\d{2})$`)
  77. FULLISO_TIME_REG2 = regexp.MustCompile(`^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d{3,9}(Z|[+-]\d{2}:\d{2})$`)
  78. FULLISO_TIME_REG3 = regexp.MustCompile(`^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d{3,9}$`)
  79. COMPACT_TIME_REG = regexp.MustCompile(`^\d{14}$`)
  80. ZSTACK_TIME_REG = regexp.MustCompile(`^\w+ \d{1,2}, \d{4} \d{1,2}:\d{1,2}:\d{1,2} (AM|PM)$`) //ZStack time format "Apr 1, 2019 3:23:17 PM"
  81. MYSQL_TIME_REG = regexp.MustCompile(`^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$`)
  82. CLICKHOUSE_TIME_REG = regexp.MustCompile(`^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} [+-]\d{4} [A-Z]{3}$`)
  83. NORMAL_TIME_REG = regexp.MustCompile(`^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}$`)
  84. FULLNORMAL_TIME_REG = regexp.MustCompile(`^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{6}$`)
  85. RFC2882_TIME_REG = regexp.MustCompile(`[A-Z][a-z]{2}, [0-9]{1,2} [A-Z][a-z]{2} [0-9]{4} [0-9]{2}:[0-9]{2}:[0-9]{2} [A-Z]{3}`)
  86. // Tue May 7 15:46:33 2024
  87. CEPH_TIME_REG = regexp.MustCompile(`[A-Z][a-z]{2} [A-Z][a-z]{2} [ 123][0-9] [0-9]{2}:[0-9]{2}:[0-9]{2} [0-9]{4}`)
  88. EMAIL_REG = regexp.MustCompile(`^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$`)
  89. CHINA_MOBILE_REG = regexp.MustCompile(`^1[0-9-]{10}$`)
  90. FS_FORMAT_REG = regexp.MustCompile(`^(ext|fat|hfs|xfs|swap|ntfs|reiserfs|ufs|btrfs)`)
  91. US_CURRENCY_REG = regexp.MustCompile(`^[+-]?(\d{0,3}|((\d{1,3},)+\d{3}))(\.\d*)?$`)
  92. EU_CURRENCY_REG = regexp.MustCompile(`^[+-]?(\d{0,3}|((\d{1,3}\.)+\d{3}))(,\d*)?$`)
  93. }
  94. func MatchFunction(str string) bool {
  95. return FUNCTION_REG.MatchString(str)
  96. }
  97. func MatchUUID(str string) bool {
  98. return UUID_REG.MatchString(str)
  99. }
  100. func MatchUUIDExact(str string) bool {
  101. return UUID_EXACT_REG.MatchString(str)
  102. }
  103. func MatchInteger(str string) bool {
  104. return INTEGER_REG.MatchString(str)
  105. }
  106. func MatchFloat(str string) bool {
  107. return FLOAT_REG.MatchString(str)
  108. }
  109. func MatchMacAddr(str string) bool {
  110. return MACADDR_REG.MatchString(str)
  111. }
  112. func MatchCompactMacAddr(str string) bool {
  113. return COMPACT_MACADDR_REG.MatchString(str)
  114. }
  115. func MatchIP4Addr(str string) bool {
  116. ip := net.ParseIP(str)
  117. return ip != nil && !strings.Contains(str, ":")
  118. }
  119. func MatchCIDR(str string) bool {
  120. ip, _, err := net.ParseCIDR(str)
  121. if err != nil {
  122. return false
  123. }
  124. return ip != nil && !strings.Contains(str, ":")
  125. }
  126. func MatchCIDR6(str string) bool {
  127. ip, _, err := net.ParseCIDR(str)
  128. if err != nil {
  129. return false
  130. }
  131. return ip != nil && !strings.Contains(str, ".")
  132. }
  133. func MatchIP6Addr(str string) bool {
  134. ip := net.ParseIP(str)
  135. return ip != nil && strings.Contains(str, ":")
  136. }
  137. func MatchIPAddr(str string) bool {
  138. ip := net.ParseIP(str)
  139. return ip != nil
  140. }
  141. func MatchPtr(str string) bool {
  142. return NSPTR_REG.MatchString(str)
  143. }
  144. func MatchName(str string) bool {
  145. return NAME_REG.MatchString(str)
  146. }
  147. func MatchDomainName(str string) bool {
  148. if str == "" || len(strings.Replace(str, ".", "", -1)) > 255 {
  149. return false
  150. }
  151. return !MatchIPAddr(str) && DOMAINNAME_REG.MatchString(str)
  152. }
  153. func MatchDomainSRV(str string) bool {
  154. if !MatchDomainName(str) {
  155. return false
  156. }
  157. // Ref: https://tools.ietf.org/html/rfc2782
  158. //
  159. // _Service._Proto.Name
  160. parts := strings.SplitN(str, ".", 3)
  161. if len(parts) != 3 {
  162. return false
  163. }
  164. for i := 0; i < 2; i++ {
  165. if len(parts[i]) < 2 || parts[i][0] != '_' {
  166. return false
  167. }
  168. }
  169. if len(parts[2]) == 0 {
  170. return false
  171. }
  172. return true
  173. }
  174. func MatchSize(str string) bool {
  175. return SIZE_REG.MatchString(str)
  176. }
  177. func MatchMonth(str string) bool {
  178. return MONTH_REG.MatchString(str)
  179. }
  180. func MatchDate(str string) bool {
  181. return DATE_REG.MatchString(str)
  182. }
  183. func MatchDateCompact(str string) bool {
  184. return DATE_COMPACT_REG.MatchString(str)
  185. }
  186. func MatchDateExcel(str string) bool {
  187. return DATE_EXCEL_REG.MatchString(str)
  188. }
  189. func MatchZStackTime(str string) bool {
  190. return ZSTACK_TIME_REG.MatchString(str)
  191. }
  192. func MatchISOTime(str string) bool {
  193. return ISO_TIME_REG.MatchString(str)
  194. }
  195. func MatchISONoSecondTime(str string) bool {
  196. return ISO_NO_SECOND_TIME_REG.MatchString(str)
  197. }
  198. func MatchFullISOTime(str string) bool {
  199. return FULLISO_TIME_REG.MatchString(str)
  200. }
  201. func MatchISOTime2(str string) bool {
  202. return ISO_TIME_REG2.MatchString(str)
  203. }
  204. func MatchISONoSecondTime2(str string) bool {
  205. return ISO_NO_SECOND_TIME_REG2.MatchString(str)
  206. }
  207. func MatchFullISOTime2(str string) bool {
  208. return FULLISO_TIME_REG2.MatchString(str)
  209. }
  210. func MatchFullISOTime3(str string) bool {
  211. return FULLISO_TIME_REG3.MatchString(str)
  212. }
  213. func MatchCompactTime(str string) bool {
  214. return COMPACT_TIME_REG.MatchString(str)
  215. }
  216. func MatchMySQLTime(str string) bool {
  217. return MYSQL_TIME_REG.MatchString(str)
  218. }
  219. func MatchClickhouseTime(str string) bool {
  220. return CLICKHOUSE_TIME_REG.MatchString(str)
  221. }
  222. func MatchNormalTime(str string) bool {
  223. return NORMAL_TIME_REG.MatchString(str)
  224. }
  225. func MatchFullNormalTime(str string) bool {
  226. return FULLNORMAL_TIME_REG.MatchString(str)
  227. }
  228. func MatchRFC2882Time(str string) bool {
  229. return RFC2882_TIME_REG.MatchString(str)
  230. }
  231. func MatchCephTime(str string) bool {
  232. return CEPH_TIME_REG.MatchString(str)
  233. }
  234. func MatchEmail(str string) bool {
  235. return EMAIL_REG.MatchString(str)
  236. }
  237. func MatchMobile(str string) bool {
  238. return CHINA_MOBILE_REG.MatchString(str)
  239. }
  240. func MatchFS(str string) bool {
  241. return FS_FORMAT_REG.MatchString(str)
  242. }
  243. func MatchUSCurrency(str string) bool {
  244. return US_CURRENCY_REG.MatchString(str)
  245. }
  246. func MatchEUCurrency(str string) bool {
  247. return EU_CURRENCY_REG.MatchString(str)
  248. }