regexes.go (12545B)
1 package validator 2 3 import "regexp" 4 5 const ( 6 alphaRegexString = "^[a-zA-Z]+$" 7 alphaNumericRegexString = "^[a-zA-Z0-9]+$" 8 alphaUnicodeRegexString = "^[\\p{L}]+$" 9 alphaUnicodeNumericRegexString = "^[\\p{L}\\p{N}]+$" 10 numericRegexString = "^[-+]?[0-9]+(?:\\.[0-9]+)?$" 11 numberRegexString = "^[0-9]+$" 12 hexadecimalRegexString = "^(0[xX])?[0-9a-fA-F]+$" 13 hexColorRegexString = "^#(?:[0-9a-fA-F]{3}|[0-9a-fA-F]{4}|[0-9a-fA-F]{6}|[0-9a-fA-F]{8})$" 14 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*\\)$" 15 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*\\)$" 16 hslRegexString = "^hsl\\(\\s*(?:0|[1-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:[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}])))\\.?$" 19 e164RegexString = "^\\+[1-9]?[0-9]{7,14}$" 20 base64RegexString = "^(?:[A-Za-z0-9+\\/]{4})*(?:[A-Za-z0-9+\\/]{2}==|[A-Za-z0-9+\\/]{3}=|[A-Za-z0-9+\\/]{4})$" 21 base64URLRegexString = "^(?:[A-Za-z0-9-_]{4})*(?:[A-Za-z0-9-_]{2}==|[A-Za-z0-9-_]{3}=|[A-Za-z0-9-_]{4})$" 22 base64RawURLRegexString = "^(?:[A-Za-z0-9-_]{4})*(?:[A-Za-z0-9-_]{2,4})$" 23 iSBN10RegexString = "^(?:[0-9]{9}X|[0-9]{10})$" 24 iSBN13RegexString = "^(?:(?:97(?:8|9))[0-9]{10})$" 25 uUID3RegexString = "^[0-9a-f]{8}-[0-9a-f]{4}-3[0-9a-f]{3}-[0-9a-f]{4}-[0-9a-f]{12}$" 26 uUID4RegexString = "^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$" 27 uUID5RegexString = "^[0-9a-f]{8}-[0-9a-f]{4}-5[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$" 28 uUIDRegexString = "^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$" 29 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}$" 30 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}$" 31 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}$" 32 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}$" 33 uLIDRegexString = "^[A-HJKMNP-TV-Z0-9]{26}$" 34 md4RegexString = "^[0-9a-f]{32}$" 35 md5RegexString = "^[0-9a-f]{32}$" 36 sha256RegexString = "^[0-9a-f]{64}$" 37 sha384RegexString = "^[0-9a-f]{96}$" 38 sha512RegexString = "^[0-9a-f]{128}$" 39 ripemd128RegexString = "^[0-9a-f]{32}$" 40 ripemd160RegexString = "^[0-9a-f]{40}$" 41 tiger128RegexString = "^[0-9a-f]{32}$" 42 tiger160RegexString = "^[0-9a-f]{40}$" 43 tiger192RegexString = "^[0-9a-f]{48}$" 44 aSCIIRegexString = "^[\x00-\x7F]*$" 45 printableASCIIRegexString = "^[\x20-\x7E]*$" 46 multibyteRegexString = "[^\x00-\x7F]" 47 dataURIRegexString = `^data:((?:\w+\/(?:([^;]|;[^;]).)+)?)` 48 latitudeRegexString = "^[-+]?([1-8]?\\d(\\.\\d+)?|90(\\.0+)?)$" 49 longitudeRegexString = "^[-+]?(180(\\.0+)?|((1[0-7]\\d)|([1-9]?\\d))(\\.\\d+)?)$" 50 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])$` 51 hostnameRegexStringRFC952 = `^[a-zA-Z]([a-zA-Z0-9\-]+[\.]?)*[a-zA-Z0-9]$` // https://tools.ietf.org/html/rfc952 52 hostnameRegexStringRFC1123 = `^([a-zA-Z0-9]{1}[a-zA-Z0-9-]{0,62}){1}(\.[a-zA-Z0-9]{1}[a-zA-Z0-9-]{0,62})*?$` // accepts hostname starting with a digit https://tools.ietf.org/html/rfc1123 53 fqdnRegexStringRFC1123 = `^([a-zA-Z0-9]{1}[a-zA-Z0-9-]{0,62})(\.[a-zA-Z0-9]{1}[a-zA-Z0-9-]{0,62})*?(\.[a-zA-Z]{1}[a-zA-Z0-9]{0,62})\.?$` // same as hostnameRegexStringRFC1123 but must contain a non numerical TLD (possibly ending with '.') 54 btcAddressRegexString = `^[13][a-km-zA-HJ-NP-Z1-9]{25,34}$` // bitcoin address 55 btcAddressUpperRegexStringBech32 = `^BC1[02-9AC-HJ-NP-Z]{7,76}$` // bitcoin bech32 address https://en.bitcoin.it/wiki/Bech32 56 btcAddressLowerRegexStringBech32 = `^bc1[02-9ac-hj-np-z]{7,76}$` // bitcoin bech32 address https://en.bitcoin.it/wiki/Bech32 57 ethAddressRegexString = `^0x[0-9a-fA-F]{40}$` 58 ethAddressUpperRegexString = `^0x[0-9A-F]{40}$` 59 ethAddressLowerRegexString = `^0x[0-9a-f]{40}$` 60 uRLEncodedRegexString = `^(?:[^%]|%[0-9A-Fa-f]{2})*$` 61 hTMLEncodedRegexString = `&#[x]?([0-9a-fA-F]{2})|(>)|(<)|(")|(&)+[;]?` 62 hTMLRegexString = `<[/]?([a-zA-Z]+).*?>` 63 jWTRegexString = "^[A-Za-z0-9-_]+\\.[A-Za-z0-9-_]+\\.[A-Za-z0-9-_]*$" 64 splitParamsRegexString = `'[^']*'|\S+` 65 bicRegexString = `^[A-Za-z]{6}[A-Za-z0-9]{2}([A-Za-z0-9]{3})?$` 66 semverRegexString = `^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$` // numbered capture groups https://semver.org/ 67 dnsRegexStringRFC1035Label = "^[a-z]([-a-z0-9]*[a-z0-9]){0,62}$" 68 cveRegexString = `^CVE-(1999|2\d{3})-(0[^0]\d{2}|0\d[^0]\d{1}|0\d{2}[^0]|[1-9]{1}\d{3,})$` // CVE Format Id https://cve.mitre.org/cve/identifiers/syntaxchange.html 69 mongodbRegexString = "^[a-f\\d]{24}$" 70 cronRegexString = `(@(annually|yearly|monthly|weekly|daily|hourly|reboot))|(@every (\d+(ns|us|µs|ms|s|m|h))+)|((((\d+,)+\d+|(\d+(\/|-)\d+)|\d+|\*) ?){5,7})` 71 ) 72 73 var ( 74 alphaRegex = regexp.MustCompile(alphaRegexString) 75 alphaNumericRegex = regexp.MustCompile(alphaNumericRegexString) 76 alphaUnicodeRegex = regexp.MustCompile(alphaUnicodeRegexString) 77 alphaUnicodeNumericRegex = regexp.MustCompile(alphaUnicodeNumericRegexString) 78 numericRegex = regexp.MustCompile(numericRegexString) 79 numberRegex = regexp.MustCompile(numberRegexString) 80 hexadecimalRegex = regexp.MustCompile(hexadecimalRegexString) 81 hexColorRegex = regexp.MustCompile(hexColorRegexString) 82 rgbRegex = regexp.MustCompile(rgbRegexString) 83 rgbaRegex = regexp.MustCompile(rgbaRegexString) 84 hslRegex = regexp.MustCompile(hslRegexString) 85 hslaRegex = regexp.MustCompile(hslaRegexString) 86 e164Regex = regexp.MustCompile(e164RegexString) 87 emailRegex = regexp.MustCompile(emailRegexString) 88 base64Regex = regexp.MustCompile(base64RegexString) 89 base64URLRegex = regexp.MustCompile(base64URLRegexString) 90 base64RawURLRegex = regexp.MustCompile(base64RawURLRegexString) 91 iSBN10Regex = regexp.MustCompile(iSBN10RegexString) 92 iSBN13Regex = regexp.MustCompile(iSBN13RegexString) 93 uUID3Regex = regexp.MustCompile(uUID3RegexString) 94 uUID4Regex = regexp.MustCompile(uUID4RegexString) 95 uUID5Regex = regexp.MustCompile(uUID5RegexString) 96 uUIDRegex = regexp.MustCompile(uUIDRegexString) 97 uUID3RFC4122Regex = regexp.MustCompile(uUID3RFC4122RegexString) 98 uUID4RFC4122Regex = regexp.MustCompile(uUID4RFC4122RegexString) 99 uUID5RFC4122Regex = regexp.MustCompile(uUID5RFC4122RegexString) 100 uUIDRFC4122Regex = regexp.MustCompile(uUIDRFC4122RegexString) 101 uLIDRegex = regexp.MustCompile(uLIDRegexString) 102 md4Regex = regexp.MustCompile(md4RegexString) 103 md5Regex = regexp.MustCompile(md5RegexString) 104 sha256Regex = regexp.MustCompile(sha256RegexString) 105 sha384Regex = regexp.MustCompile(sha384RegexString) 106 sha512Regex = regexp.MustCompile(sha512RegexString) 107 ripemd128Regex = regexp.MustCompile(ripemd128RegexString) 108 ripemd160Regex = regexp.MustCompile(ripemd160RegexString) 109 tiger128Regex = regexp.MustCompile(tiger128RegexString) 110 tiger160Regex = regexp.MustCompile(tiger160RegexString) 111 tiger192Regex = regexp.MustCompile(tiger192RegexString) 112 aSCIIRegex = regexp.MustCompile(aSCIIRegexString) 113 printableASCIIRegex = regexp.MustCompile(printableASCIIRegexString) 114 multibyteRegex = regexp.MustCompile(multibyteRegexString) 115 dataURIRegex = regexp.MustCompile(dataURIRegexString) 116 latitudeRegex = regexp.MustCompile(latitudeRegexString) 117 longitudeRegex = regexp.MustCompile(longitudeRegexString) 118 sSNRegex = regexp.MustCompile(sSNRegexString) 119 hostnameRegexRFC952 = regexp.MustCompile(hostnameRegexStringRFC952) 120 hostnameRegexRFC1123 = regexp.MustCompile(hostnameRegexStringRFC1123) 121 fqdnRegexRFC1123 = regexp.MustCompile(fqdnRegexStringRFC1123) 122 btcAddressRegex = regexp.MustCompile(btcAddressRegexString) 123 btcUpperAddressRegexBech32 = regexp.MustCompile(btcAddressUpperRegexStringBech32) 124 btcLowerAddressRegexBech32 = regexp.MustCompile(btcAddressLowerRegexStringBech32) 125 ethAddressRegex = regexp.MustCompile(ethAddressRegexString) 126 uRLEncodedRegex = regexp.MustCompile(uRLEncodedRegexString) 127 hTMLEncodedRegex = regexp.MustCompile(hTMLEncodedRegexString) 128 hTMLRegex = regexp.MustCompile(hTMLRegexString) 129 jWTRegex = regexp.MustCompile(jWTRegexString) 130 splitParamsRegex = regexp.MustCompile(splitParamsRegexString) 131 bicRegex = regexp.MustCompile(bicRegexString) 132 semverRegex = regexp.MustCompile(semverRegexString) 133 dnsRegexRFC1035Label = regexp.MustCompile(dnsRegexStringRFC1035Label) 134 cveRegex = regexp.MustCompile(cveRegexString) 135 mongodbRegex = regexp.MustCompile(mongodbRegexString) 136 cronRegex = regexp.MustCompile(cronRegexString) 137 )