/vendor/yunion.io/x/pkg/util/regutils/regutils.go

https://github.com/yunionio/onecloud · Go · 243 lines · 189 code · 38 blank · 16 comment · 22 complexity · beea54f29b8ab047ea2d8e79934ee3e4 MD5 · raw file

  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 ISO_TIME_REG *regexp.Regexp
  36. var ISO_NO_SECOND_TIME_REG *regexp.Regexp
  37. var FULLISO_TIME_REG *regexp.Regexp
  38. var ZSTACK_TIME_REG *regexp.Regexp
  39. var COMPACT_TIME_REG *regexp.Regexp
  40. var MYSQL_TIME_REG *regexp.Regexp
  41. var NORMAL_TIME_REG *regexp.Regexp
  42. var FULLNORMAL_TIME_REG *regexp.Regexp
  43. var RFC2882_TIME_REG *regexp.Regexp
  44. var EMAIL_REG *regexp.Regexp
  45. var CHINA_MOBILE_REG *regexp.Regexp
  46. var FS_FORMAT_REG *regexp.Regexp
  47. var US_CURRENCY_REG *regexp.Regexp
  48. var EU_CURRENCY_REG *regexp.Regexp
  49. func init() {
  50. FUNCTION_REG = regexp.MustCompile(`^\w+\(.*\)$`)
  51. UUID_REG = regexp.MustCompile(`[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}`)
  52. 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}$`)
  53. INTEGER_REG = regexp.MustCompile(`^[0-9]+$`)
  54. FLOAT_REG = regexp.MustCompile(`^\d+(\.\d*)?$`)
  55. MACADDR_REG = regexp.MustCompile(`^([0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}$`)
  56. COMPACT_MACADDR_REG = regexp.MustCompile(`^[0-9a-fA-F]{12}$`)
  57. NSPTR_REG = regexp.MustCompile(`^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\.in-addr\.arpa$`)
  58. NAME_REG = regexp.MustCompile(`^[a-zA-Z][a-zA-Z0-9._@-]*$`)
  59. 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})*[\._]?$`)
  60. SIZE_REG = regexp.MustCompile(`^\d+[bBkKmMgG]?$`)
  61. MONTH_REG = regexp.MustCompile(`^\d{4}-\d{2}$`)
  62. DATE_REG = regexp.MustCompile(`^\d{4}-\d{2}-\d{2}$`)
  63. DATE_COMPACT_REG = regexp.MustCompile(`^\d{8}$`)
  64. ISO_TIME_REG = regexp.MustCompile(`^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(Z|[+-]\d{2}:\d{2})$`)
  65. ISO_NO_SECOND_TIME_REG = regexp.MustCompile(`^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}(Z|[+-]\d{2}:\d{2})$`)
  66. 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})$`)
  67. COMPACT_TIME_REG = regexp.MustCompile(`^\d{14}$`)
  68. 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"
  69. MYSQL_TIME_REG = regexp.MustCompile(`^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$`)
  70. NORMAL_TIME_REG = regexp.MustCompile(`^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}$`)
  71. FULLNORMAL_TIME_REG = regexp.MustCompile(`^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{6}$`)
  72. 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}`)
  73. EMAIL_REG = regexp.MustCompile(`^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$`)
  74. CHINA_MOBILE_REG = regexp.MustCompile(`^1[0-9-]{10}$`)
  75. FS_FORMAT_REG = regexp.MustCompile(`^(ext|fat|hfs|xfs|swap|ntfs|reiserfs|ufs|btrfs)`)
  76. US_CURRENCY_REG = regexp.MustCompile(`^[+-]?(\d{0,3}|((\d{1,3},)+\d{3}))(\.\d*)?$`)
  77. EU_CURRENCY_REG = regexp.MustCompile(`^[+-]?(\d{0,3}|((\d{1,3}\.)+\d{3}))(,\d*)?$`)
  78. }
  79. func MatchFunction(str string) bool {
  80. return FUNCTION_REG.MatchString(str)
  81. }
  82. func MatchUUID(str string) bool {
  83. return UUID_REG.MatchString(str)
  84. }
  85. func MatchUUIDExact(str string) bool {
  86. return UUID_EXACT_REG.MatchString(str)
  87. }
  88. func MatchInteger(str string) bool {
  89. return INTEGER_REG.MatchString(str)
  90. }
  91. func MatchFloat(str string) bool {
  92. return FLOAT_REG.MatchString(str)
  93. }
  94. func MatchMacAddr(str string) bool {
  95. return MACADDR_REG.MatchString(str)
  96. }
  97. func MatchCompactMacAddr(str string) bool {
  98. return COMPACT_MACADDR_REG.MatchString(str)
  99. }
  100. func MatchIP4Addr(str string) bool {
  101. ip := net.ParseIP(str)
  102. return ip != nil && !strings.Contains(str, ":")
  103. }
  104. func MatchCIDR(str string) bool {
  105. ip, _, err := net.ParseCIDR(str)
  106. if err != nil {
  107. return false
  108. }
  109. return ip != nil && !strings.Contains(str, ":")
  110. }
  111. func MatchIP6Addr(str string) bool {
  112. ip := net.ParseIP(str)
  113. return ip != nil && strings.Contains(str, ":")
  114. }
  115. func MatchIPAddr(str string) bool {
  116. ip := net.ParseIP(str)
  117. return ip != nil
  118. }
  119. func MatchPtr(str string) bool {
  120. return NSPTR_REG.MatchString(str)
  121. }
  122. func MatchName(str string) bool {
  123. return NAME_REG.MatchString(str)
  124. }
  125. func MatchDomainName(str string) bool {
  126. if str == "" || len(strings.Replace(str, ".", "", -1)) > 255 {
  127. return false
  128. }
  129. return !MatchIPAddr(str) && DOMAINNAME_REG.MatchString(str)
  130. }
  131. func MatchDomainSRV(str string) bool {
  132. if !MatchDomainName(str) {
  133. return false
  134. }
  135. // Ref: https://tools.ietf.org/html/rfc2782
  136. //
  137. // _Service._Proto.Name
  138. parts := strings.SplitN(str, ".", 3)
  139. if len(parts) != 3 {
  140. return false
  141. }
  142. for i := 0; i < 2; i++ {
  143. if len(parts[i]) < 2 || parts[i][0] != '_' {
  144. return false
  145. }
  146. }
  147. if len(parts[2]) == 0 {
  148. return false
  149. }
  150. return true
  151. }
  152. func MatchSize(str string) bool {
  153. return SIZE_REG.MatchString(str)
  154. }
  155. func MatchMonth(str string) bool {
  156. return MONTH_REG.MatchString(str)
  157. }
  158. func MatchDate(str string) bool {
  159. return DATE_REG.MatchString(str)
  160. }
  161. func MatchDateCompact(str string) bool {
  162. return DATE_COMPACT_REG.MatchString(str)
  163. }
  164. func MatchZStackTime(str string) bool {
  165. return ZSTACK_TIME_REG.MatchString(str)
  166. }
  167. func MatchISOTime(str string) bool {
  168. return ISO_TIME_REG.MatchString(str)
  169. }
  170. func MatchISONoSecondTime(str string) bool {
  171. return ISO_NO_SECOND_TIME_REG.MatchString(str)
  172. }
  173. func MatchFullISOTime(str string) bool {
  174. return FULLISO_TIME_REG.MatchString(str)
  175. }
  176. func MatchCompactTime(str string) bool {
  177. return COMPACT_TIME_REG.MatchString(str)
  178. }
  179. func MatchMySQLTime(str string) bool {
  180. return MYSQL_TIME_REG.MatchString(str)
  181. }
  182. func MatchNormalTime(str string) bool {
  183. return NORMAL_TIME_REG.MatchString(str)
  184. }
  185. func MatchFullNormalTime(str string) bool {
  186. return FULLNORMAL_TIME_REG.MatchString(str)
  187. }
  188. func MatchRFC2882Time(str string) bool {
  189. return RFC2882_TIME_REG.MatchString(str)
  190. }
  191. func MatchEmail(str string) bool {
  192. return EMAIL_REG.MatchString(str)
  193. }
  194. func MatchMobile(str string) bool {
  195. return CHINA_MOBILE_REG.MatchString(str)
  196. }
  197. func MatchFS(str string) bool {
  198. return FS_FORMAT_REG.MatchString(str)
  199. }
  200. func MatchUSCurrency(str string) bool {
  201. return US_CURRENCY_REG.MatchString(str)
  202. }
  203. func MatchEUCurrency(str string) bool {
  204. return EU_CURRENCY_REG.MatchString(str)
  205. }