git.sr.ht/~pingoo/stdx@v0.0.0-20240218134121-094174641f6e/validate/patterns.go (about)

     1  package validate
     2  
     3  import "regexp"
     4  
     5  // Basic regular expressions for validating strings
     6  const (
     7  	Email             string = "^(((([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}])+)*)|((\\x22)((((\\x20|\\x09)*(\\x0d\\x0a))?(\\x20|\\x09)+)?(([\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x7f]|\\x21|[\\x23-\\x5b]|[\\x5d-\\x7e]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])|(\\([\\x01-\\x09\\x0b\\x0c\\x0d-\\x7f]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}]))))*(((\\x20|\\x09)*(\\x0d\\x0a))?(\\x20|\\x09)+)?(\\x22)))@((([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]|\\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}])))\\.?$"
     8  	CreditCard        string = "^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|(222[1-9]|22[3-9][0-9]|2[3-6][0-9]{2}|27[01][0-9]|2720)[0-9]{12}|6(?:011|5[0-9][0-9])[0-9]{12}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\\d{3})\\d{11}|6[27][0-9]{14})$"
     9  	ISBN10            string = "^(?:[0-9]{9}X|[0-9]{10})$"
    10  	ISBN13            string = "^(?:[0-9]{13})$"
    11  	UUID3             string = "^[0-9a-f]{8}-[0-9a-f]{4}-3[0-9a-f]{3}-[0-9a-f]{4}-[0-9a-f]{12}$"
    12  	UUID4             string = "^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$"
    13  	UUID5             string = "^[0-9a-f]{8}-[0-9a-f]{4}-5[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$"
    14  	UUID              string = "^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$"
    15  	Alpha             string = "^[a-zA-Z]+$"
    16  	Alphanumeric      string = "^[a-zA-Z0-9]+$"
    17  	Numeric           string = "^[0-9]+$"
    18  	Int               string = "^(?:[-+]?(?:0|[1-9][0-9]*))$"
    19  	Float             string = "^(?:[-+]?(?:[0-9]+))?(?:\\.[0-9]*)?(?:[eE][\\+\\-]?(?:[0-9]+))?$"
    20  	Hexadecimal       string = "^[0-9a-fA-F]+$"
    21  	Hexcolor          string = "^#?([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$"
    22  	RGBcolor          string = "^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])\\s*\\)$"
    23  	ASCII             string = "^[\x00-\x7F]+$"
    24  	Multibyte         string = "[^\x00-\x7F]"
    25  	FullWidth         string = "[^\u0020-\u007E\uFF61-\uFF9F\uFFA0-\uFFDC\uFFE8-\uFFEE0-9a-zA-Z]"
    26  	HalfWidth         string = "[\u0020-\u007E\uFF61-\uFF9F\uFFA0-\uFFDC\uFFE8-\uFFEE0-9a-zA-Z]"
    27  	Base64            string = "^(?:[A-Za-z0-9+\\/]{4})*(?:[A-Za-z0-9+\\/]{2}==|[A-Za-z0-9+\\/]{3}=|[A-Za-z0-9+\\/]{4})$"
    28  	PrintableASCII    string = "^[\x20-\x7E]+$"
    29  	DataURI           string = "^data:.+\\/(.+);base64$"
    30  	MagnetURI         string = "^magnet:\\?xt=urn:[a-zA-Z0-9]+:[a-zA-Z0-9]{32,40}&dn=.+&tr=.+$"
    31  	Latitude          string = "^[-+]?([1-8]?\\d(\\.\\d+)?|90(\\.0+)?)$"
    32  	Longitude         string = "^[-+]?(180(\\.0+)?|((1[0-7]\\d)|([1-9]?\\d))(\\.\\d+)?)$"
    33  	DNSName           string = `^([a-zA-Z0-9_]{1}[a-zA-Z0-9_-]{0,62}){1}(\.[a-zA-Z0-9_]{1}[a-zA-Z0-9_-]{0,62})*[\._]?$`
    34  	IP                string = `(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))`
    35  	URLSchema         string = `((ftp|tcp|udp|wss?|https?):\/\/)`
    36  	URLUsername       string = `(\S+(:\S*)?@)`
    37  	URLPath           string = `((\/|\?|#)[^\s]*)`
    38  	URLPort           string = `(:(\d{1,5}))`
    39  	URLIP             string = `([1-9]\d?|1\d\d|2[01]\d|22[0-3]|24\d|25[0-5])(\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])){2}(?:\.([0-9]\d?|1\d\d|2[0-4]\d|25[0-5]))`
    40  	URLSubdomain      string = `((www\.)|([a-zA-Z0-9]+([-_\.]?[a-zA-Z0-9])*[a-zA-Z0-9]\.[a-zA-Z0-9]+))`
    41  	URL                      = `^` + URLSchema + `?` + URLUsername + `?` + `((` + URLIP + `|(\[` + IP + `\])|(([a-zA-Z0-9]([a-zA-Z0-9-_]+)?[a-zA-Z0-9]([-\.][a-zA-Z0-9]+)*)|(` + URLSubdomain + `?))?(([a-zA-Z\x{00a1}-\x{ffff}0-9]+-?-?)*[a-zA-Z\x{00a1}-\x{ffff}0-9]+)(?:\.([a-zA-Z\x{00a1}-\x{ffff}]{1,}))?))\.?` + URLPort + `?` + URLPath + `?$`
    42  	SSN               string = `^\d{3}[- ]?\d{2}[- ]?\d{4}$`
    43  	WinPath           string = `^[a-zA-Z]:\\(?:[^\\/:*?"<>|\r\n]+\\)*[^\\/:*?"<>|\r\n]*$`
    44  	UnixPath          string = `^(/[^/\x00]*)+/?$`
    45  	WinARPath         string = `^(?:(?:[a-zA-Z]:|\\\\[a-z0-9_.$●-]+\\[a-z0-9_.$●-]+)\\|\\?[^\\/:*?"<>|\r\n]+\\?)(?:[^\\/:*?"<>|\r\n]+\\)*[^\\/:*?"<>|\r\n]*$`
    46  	UnixARPath        string = `^((\.{0,2}/)?([^/\x00]*))+/?$`
    47  	Semver            string = "^v?(?: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-]+)*)?$"
    48  	tagName           string = "valid"
    49  	hasLowerCase      string = ".*[[:lower:]]"
    50  	hasUpperCase      string = ".*[[:upper:]]"
    51  	hasWhitespace     string = ".*[[:space:]]"
    52  	hasWhitespaceOnly string = "^[[:space:]]+$"
    53  	IMEI              string = "^[0-9a-f]{14}$|^\\d{15}$|^\\d{18}$"
    54  	IMSI              string = "^\\d{14,15}$"
    55  	E164              string = `^\+?[1-9]\d{1,14}$`
    56  )
    57  
    58  // Used by IsFilePath func
    59  const (
    60  	// Unknown is unresolved OS type
    61  	Unknown = iota
    62  	// Win is Windows type
    63  	Win
    64  	// Unix is *nix OS types
    65  	Unix
    66  )
    67  
    68  var (
    69  	userRegexp          = regexp.MustCompile("^[a-zA-Z0-9!#$%&'*+/=?^_`{|}~.-]+$")
    70  	hostRegexp          = regexp.MustCompile("^[^\\s]+\\.[^\\s]+$")
    71  	userDotRegexp       = regexp.MustCompile("(^[.]{1})|([.]{1}$)|([.]{2,})")
    72  	rxEmail             = regexp.MustCompile(Email)
    73  	rxCreditCard        = regexp.MustCompile(CreditCard)
    74  	rxISBN10            = regexp.MustCompile(ISBN10)
    75  	rxISBN13            = regexp.MustCompile(ISBN13)
    76  	rxUUID3             = regexp.MustCompile(UUID3)
    77  	rxUUID4             = regexp.MustCompile(UUID4)
    78  	rxUUID5             = regexp.MustCompile(UUID5)
    79  	rxUUID              = regexp.MustCompile(UUID)
    80  	rxAlpha             = regexp.MustCompile(Alpha)
    81  	rxAlphanumeric      = regexp.MustCompile(Alphanumeric)
    82  	rxNumeric           = regexp.MustCompile(Numeric)
    83  	rxInt               = regexp.MustCompile(Int)
    84  	rxFloat             = regexp.MustCompile(Float)
    85  	rxHexadecimal       = regexp.MustCompile(Hexadecimal)
    86  	rxHexcolor          = regexp.MustCompile(Hexcolor)
    87  	rxRGBcolor          = regexp.MustCompile(RGBcolor)
    88  	rxASCII             = regexp.MustCompile(ASCII)
    89  	rxPrintableASCII    = regexp.MustCompile(PrintableASCII)
    90  	rxMultibyte         = regexp.MustCompile(Multibyte)
    91  	rxFullWidth         = regexp.MustCompile(FullWidth)
    92  	rxHalfWidth         = regexp.MustCompile(HalfWidth)
    93  	rxBase64            = regexp.MustCompile(Base64)
    94  	rxDataURI           = regexp.MustCompile(DataURI)
    95  	rxMagnetURI         = regexp.MustCompile(MagnetURI)
    96  	rxLatitude          = regexp.MustCompile(Latitude)
    97  	rxLongitude         = regexp.MustCompile(Longitude)
    98  	rxDNSName           = regexp.MustCompile(DNSName)
    99  	rxURL               = regexp.MustCompile(URL)
   100  	rxSSN               = regexp.MustCompile(SSN)
   101  	rxWinPath           = regexp.MustCompile(WinPath)
   102  	rxUnixPath          = regexp.MustCompile(UnixPath)
   103  	rxARWinPath         = regexp.MustCompile(WinARPath)
   104  	rxARUnixPath        = regexp.MustCompile(UnixARPath)
   105  	rxSemver            = regexp.MustCompile(Semver)
   106  	rxHasLowerCase      = regexp.MustCompile(hasLowerCase)
   107  	rxHasUpperCase      = regexp.MustCompile(hasUpperCase)
   108  	rxHasWhitespace     = regexp.MustCompile(hasWhitespace)
   109  	rxHasWhitespaceOnly = regexp.MustCompile(hasWhitespaceOnly)
   110  	rxIMEI              = regexp.MustCompile(IMEI)
   111  	rxIMSI              = regexp.MustCompile(IMSI)
   112  	rxE164              = regexp.MustCompile(E164)
   113  )