regexes.go 8.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package validator
  2. import "regexp"
  3. const (
  4. alphaRegexString = "^[a-zA-Z]+$"
  5. alphaNumericRegexString = "^[a-zA-Z0-9]+$"
  6. alphaUnicodeRegexString = "^[\\p{L}]+$"
  7. alphaUnicodeNumericRegexString = "^[\\p{L}\\p{N}]+$"
  8. numericRegexString = "^[-+]?[0-9]+(?:\\.[0-9]+)?$"
  9. numberRegexString = "^[0-9]+$"
  10. hexadecimalRegexString = "^[0-9a-fA-F]+$"
  11. hexcolorRegexString = "^#(?:[0-9a-fA-F]{3}|[0-9a-fA-F]{6})$"
  12. rgbRegexString = "^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])|(?: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*\\)$"
  13. rgbaRegexString = "^rgba\\(\\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])|(?: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*,\\s*(?:(?:0.[1-9]*)|[01])\\s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\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])(?:[a-zA-Z]|\\d|-|\\.|~|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])*(?:[a-zA-Z]|\\d|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])))\\.)+(?:(?:[a-zA-Z]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])|(?:(?:[a-zA-Z]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])(?:[a-zA-Z]|\\d|-|\\.|~|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])*(?:[a-zA-Z]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])))\\.?$"
  17. base64RegexString = "^(?:[A-Za-z0-9+\\/]{4})*(?:[A-Za-z0-9+\\/]{2}==|[A-Za-z0-9+\\/]{3}=|[A-Za-z0-9+\\/]{4})$"
  18. base64URLRegexString = "^(?:[A-Za-z0-9-_]{4})*(?:[A-Za-z0-9-_]{2}==|[A-Za-z0-9-_]{3}=|[A-Za-z0-9-_]{4})$"
  19. iSBN10RegexString = "^(?:[0-9]{9}X|[0-9]{10})$"
  20. iSBN13RegexString = "^(?:(?:97(?:8|9))[0-9]{10})$"
  21. uUID3RegexString = "^[0-9a-f]{8}-[0-9a-f]{4}-3[0-9a-f]{3}-[0-9a-f]{4}-[0-9a-f]{12}$"
  22. uUID4RegexString = "^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$"
  23. uUID5RegexString = "^[0-9a-f]{8}-[0-9a-f]{4}-5[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$"
  24. uUIDRegexString = "^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$"
  25. uUID3RFC4122RegexString = "^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-3[0-9a-fA-F]{3}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$"
  26. uUID4RFC4122RegexString = "^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-4[0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$"
  27. uUID5RFC4122RegexString = "^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-5[0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$"
  28. uUIDRFC4122RegexString = "^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$"
  29. aSCIIRegexString = "^[\x00-\x7F]*$"
  30. printableASCIIRegexString = "^[\x20-\x7E]*$"
  31. multibyteRegexString = "[^\x00-\x7F]"
  32. dataURIRegexString = "^data:.+\\/(.+);base64$"
  33. latitudeRegexString = "^[-+]?([1-8]?\\d(\\.\\d+)?|90(\\.0+)?)$"
  34. longitudeRegexString = "^[-+]?(180(\\.0+)?|((1[0-7]\\d)|([1-9]?\\d))(\\.\\d+)?)$"
  35. sSNRegexString = `^[0-9]{3}[ -]?(0[1-9]|[1-9][0-9])[ -]?([1-9][0-9]{3}|[0-9][1-9][0-9]{2}|[0-9]{2}[1-9][0-9]|[0-9]{3}[1-9])$`
  36. hostnameRegexStringRFC952 = `^[a-zA-Z][a-zA-Z0-9\-\.]+[a-zA-Z0-9]$` // https://tools.ietf.org/html/rfc952
  37. hostnameRegexStringRFC1123 = `^[a-zA-Z0-9][a-zA-Z0-9\-\.]+[a-zA-Z0-9]$` // accepts hostname starting with a digit https://tools.ietf.org/html/rfc1123
  38. btcAddressRegexString = `^[13][a-km-zA-HJ-NP-Z1-9]{25,34}$` // bitcoin address
  39. btcAddressUpperRegexStringBech32 = `^BC1[02-9AC-HJ-NP-Z]{7,76}$` // bitcoin bech32 address https://en.bitcoin.it/wiki/Bech32
  40. btcAddressLowerRegexStringBech32 = `^bc1[02-9ac-hj-np-z]{7,76}$` // bitcoin bech32 address https://en.bitcoin.it/wiki/Bech32
  41. ethAddressRegexString = `^0x[0-9a-fA-F]{40}$`
  42. ethAddressUpperRegexString = `^0x[0-9A-F]{40}$`
  43. ethAddressLowerRegexString = `^0x[0-9a-f]{40}$`
  44. uRLEncodedRegexString = `(%[A-Fa-f0-9]{2})`
  45. hTMLEncodedRegexString = `&#[x]?([0-9a-fA-F]{2})|(&gt)|(&lt)|(&quot)|(&amp)+[;]?`
  46. hTMLRegexString = `<[/]?([a-zA-Z]+).*?>`
  47. )
  48. var (
  49. alphaRegex = regexp.MustCompile(alphaRegexString)
  50. alphaNumericRegex = regexp.MustCompile(alphaNumericRegexString)
  51. alphaUnicodeRegex = regexp.MustCompile(alphaUnicodeRegexString)
  52. alphaUnicodeNumericRegex = regexp.MustCompile(alphaUnicodeNumericRegexString)
  53. numericRegex = regexp.MustCompile(numericRegexString)
  54. numberRegex = regexp.MustCompile(numberRegexString)
  55. hexadecimalRegex = regexp.MustCompile(hexadecimalRegexString)
  56. hexcolorRegex = regexp.MustCompile(hexcolorRegexString)
  57. rgbRegex = regexp.MustCompile(rgbRegexString)
  58. rgbaRegex = regexp.MustCompile(rgbaRegexString)
  59. hslRegex = regexp.MustCompile(hslRegexString)
  60. hslaRegex = regexp.MustCompile(hslaRegexString)
  61. emailRegex = regexp.MustCompile(emailRegexString)
  62. base64Regex = regexp.MustCompile(base64RegexString)
  63. base64URLRegex = regexp.MustCompile(base64URLRegexString)
  64. iSBN10Regex = regexp.MustCompile(iSBN10RegexString)
  65. iSBN13Regex = regexp.MustCompile(iSBN13RegexString)
  66. uUID3Regex = regexp.MustCompile(uUID3RegexString)
  67. uUID4Regex = regexp.MustCompile(uUID4RegexString)
  68. uUID5Regex = regexp.MustCompile(uUID5RegexString)
  69. uUIDRegex = regexp.MustCompile(uUIDRegexString)
  70. uUID3RFC4122Regex = regexp.MustCompile(uUID3RFC4122RegexString)
  71. uUID4RFC4122Regex = regexp.MustCompile(uUID4RFC4122RegexString)
  72. uUID5RFC4122Regex = regexp.MustCompile(uUID5RFC4122RegexString)
  73. uUIDRFC4122Regex = regexp.MustCompile(uUIDRFC4122RegexString)
  74. aSCIIRegex = regexp.MustCompile(aSCIIRegexString)
  75. printableASCIIRegex = regexp.MustCompile(printableASCIIRegexString)
  76. multibyteRegex = regexp.MustCompile(multibyteRegexString)
  77. dataURIRegex = regexp.MustCompile(dataURIRegexString)
  78. latitudeRegex = regexp.MustCompile(latitudeRegexString)
  79. longitudeRegex = regexp.MustCompile(longitudeRegexString)
  80. sSNRegex = regexp.MustCompile(sSNRegexString)
  81. hostnameRegexRFC952 = regexp.MustCompile(hostnameRegexStringRFC952)
  82. hostnameRegexRFC1123 = regexp.MustCompile(hostnameRegexStringRFC1123)
  83. btcAddressRegex = regexp.MustCompile(btcAddressRegexString)
  84. btcUpperAddressRegexBech32 = regexp.MustCompile(btcAddressUpperRegexStringBech32)
  85. btcLowerAddressRegexBech32 = regexp.MustCompile(btcAddressLowerRegexStringBech32)
  86. ethAddressRegex = regexp.MustCompile(ethAddressRegexString)
  87. ethaddressRegexUpper = regexp.MustCompile(ethAddressUpperRegexString)
  88. ethAddressRegexLower = regexp.MustCompile(ethAddressLowerRegexString)
  89. uRLEncodedRegex = regexp.MustCompile(uRLEncodedRegexString)
  90. hTMLEncodedRegex = regexp.MustCompile(hTMLEncodedRegexString)
  91. hTMLRegex = regexp.MustCompile(hTMLRegexString)
  92. )