regexes.go 5.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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*\\)$"
  14. hslRegexString = "^h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\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. iSBN10RegexString = "^(?:[0-9]{9}X|[0-9]{10})$"
  19. iSBN13RegexString = "^(?:(?:97(?:8|9))[0-9]{10})$"
  20. uUID3RegexString = "^[0-9a-f]{8}-[0-9a-f]{4}-3[0-9a-f]{3}-[0-9a-f]{4}-[0-9a-f]{12}$"
  21. uUID4RegexString = "^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$"
  22. uUID5RegexString = "^[0-9a-f]{8}-[0-9a-f]{4}-5[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$"
  23. uUIDRegexString = "^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$"
  24. aSCIIRegexString = "^[\x00-\x7F]*$"
  25. printableASCIIRegexString = "^[\x20-\x7E]*$"
  26. multibyteRegexString = "[^\x00-\x7F]"
  27. dataURIRegexString = "^data:.+\\/(.+);base64$"
  28. latitudeRegexString = "^[-+]?([1-8]?\\d(\\.\\d+)?|90(\\.0+)?)$"
  29. longitudeRegexString = "^[-+]?(180(\\.0+)?|((1[0-7]\\d)|([1-9]?\\d))(\\.\\d+)?)$"
  30. sSNRegexString = `^\d{3}[- ]?\d{2}[- ]?\d{4}$`
  31. hostnameRegexString = `^[a-zA-Z][a-zA-Z0-9\-\.]+[a-z-Az0-9]$`
  32. )
  33. var (
  34. alphaRegex = regexp.MustCompile(alphaRegexString)
  35. alphaNumericRegex = regexp.MustCompile(alphaNumericRegexString)
  36. alphaUnicodeRegex = regexp.MustCompile(alphaUnicodeRegexString)
  37. alphaUnicodeNumericRegex = regexp.MustCompile(alphaUnicodeNumericRegexString)
  38. numericRegex = regexp.MustCompile(numericRegexString)
  39. numberRegex = regexp.MustCompile(numberRegexString)
  40. hexadecimalRegex = regexp.MustCompile(hexadecimalRegexString)
  41. hexcolorRegex = regexp.MustCompile(hexcolorRegexString)
  42. rgbRegex = regexp.MustCompile(rgbRegexString)
  43. rgbaRegex = regexp.MustCompile(rgbaRegexString)
  44. hslRegex = regexp.MustCompile(hslRegexString)
  45. hslaRegex = regexp.MustCompile(hslaRegexString)
  46. emailRegex = regexp.MustCompile(emailRegexString)
  47. base64Regex = regexp.MustCompile(base64RegexString)
  48. iSBN10Regex = regexp.MustCompile(iSBN10RegexString)
  49. iSBN13Regex = regexp.MustCompile(iSBN13RegexString)
  50. uUID3Regex = regexp.MustCompile(uUID3RegexString)
  51. uUID4Regex = regexp.MustCompile(uUID4RegexString)
  52. uUID5Regex = regexp.MustCompile(uUID5RegexString)
  53. uUIDRegex = regexp.MustCompile(uUIDRegexString)
  54. aSCIIRegex = regexp.MustCompile(aSCIIRegexString)
  55. printableASCIIRegex = regexp.MustCompile(printableASCIIRegexString)
  56. multibyteRegex = regexp.MustCompile(multibyteRegexString)
  57. dataURIRegex = regexp.MustCompile(dataURIRegexString)
  58. latitudeRegex = regexp.MustCompile(latitudeRegexString)
  59. longitudeRegex = regexp.MustCompile(longitudeRegexString)
  60. sSNRegex = regexp.MustCompile(sSNRegexString)
  61. hostnameRegex = regexp.MustCompile(hostnameRegexString)
  62. )