Documentation
¶
Overview ¶
Package regex provides pre-compiled regular expressions for common data formats.
Key features:
- Email patterns (Email, Html5Email, Rfc5322Email, UnicodeEmail)
- URL patterns (URL)
- Network patterns (IPv4, IPv6, CIDR, MAC)
- Date/time patterns (Date, Time, Datetime, Duration)
- ID formats (UUID, CUID, CUID2, ULID, NanoID, XID, KSUID)
- Encoding patterns (Base64, Base64URL)
- Primitives (String, Integer, Number, Boolean)
Usage:
if regex.Email.MatchString(email) {
// valid email format
}
if regex.UUID.MatchString(uuid) {
// valid UUID format
}
Index ¶
Constants ¶
This section is empty.
Variables ¶
var Base64 = regexp.MustCompile(`^$|^(?:[0-9a-zA-Z+/]{4})*(?:(?:[0-9a-zA-Z+/]{2}==)|(?:[0-9a-zA-Z+/]{3}=))?$`)
Base64 matches Base64-encoded strings TypeScript original code: export const base64: RegExp = /^$|^(?:[0-9a-zA-Z+/]{4})*(?:(?:[0-9a-zA-Z+/]{2}==)|(?:[0-9a-zA-Z+/]{3}=))?$/;
var Base64URL = regexp.MustCompile(`^[A-Za-z0-9_-]*={0,2}$`)
Base64URL pattern allows URL-safe Base64 characters with optional "=" padding (up to 2)
var Bigint = regexp.MustCompile(`^-?\d+n?$`)
Bigint matches big integers (supports negative numbers) TypeScript original code (v4.1.11 fixed): export const bigint: RegExp = /^-?\d+n?$/;
var Boolean = regexp.MustCompile(`(?i)^(true|false)$`)
Boolean matches boolean values (true/false) TypeScript original code: export const boolean: RegExp = /true|false/i;
var BrowserEmail = regexp.MustCompile(`^[a-zA-Z0-9.!#$%&'*+/=?^_` + "`" + `{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$`)
BrowserEmail matches browser email validation TypeScript original code: export const browserEmail: RegExp =
/^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/;
var CIDRv4 = regexp.MustCompile(`^((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\/([0-9]|[1-2][0-9]|3[0-2])$`)
CIDRv4 matches IPv4 CIDR addresses TypeScript original code: export const cidrv4: RegExp =
/^((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\/([0-9]|[1-2][0-9]|3[0-2])$/;
var CIDRv6 = regexp.MustCompile(`^(([0-9a-fA-F]{1,4}:){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]))\/(12[0-8]|1[01][0-9]|[1-9]?[0-9])$`)
CIDRv6 matches IPv6 CIDR addresses Updated regex to handle more IPv6 address formats with CIDR notation
var CUID = regexp.MustCompile(`^[cC][^\s-]{8,}$`)
CUID matches strings in CUID format TypeScript original code: export const cuid: RegExp = /^[cC][^\s-]{8,}$/;
var CUID2 = regexp.MustCompile(`^[0-9a-z]+$`)
CUID2 matches strings in CUID2 format TypeScript original code: export const cuid2: RegExp = /^[0-9a-z]+$/;
var Date = regexp.MustCompile(`^` + dateSource + `$`)
Date matches ISO 8601 date format (YYYY-MM-DD) TypeScript original code: export const date: RegExp = new RegExp(`^${dateSource}$`);
var DefaultDatetime = regexp.MustCompile(`^` + dateSource + `T(?:` + timeSource(nil) + `(?:Z|[+-](?:[01]\d|2[0-3]):[0-5]\d))$`)
DefaultDatetime is the datetime regex with Z timezone only (no offsets by default) Updated to match TypeScript Zod 4 default behavior: only Z allowed, no offsets
var DefaultTime = regexp.MustCompile(`^(?:[01]\d|2[0-3]):[0-5]\d(?::[0-5]\d(?:\.\d+)?)?$`)
DefaultTime is the time regex with any decimal precision Updated to support both HH:MM and HH:MM:SS formats like TypeScript Zod 4
var Domain = regexp.MustCompile(`^([a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}$`)
Domain matches valid domain names with TLD TypeScript original code: export const domain: RegExp = /^([a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}$/;
var Duration = regexp.MustCompile(`^P(?:(\d+W)|(\d+Y)?(\d+M)?(\d+D)?(?:T(\d+H)?(\d+M)?(\d+(?:[.,]\d+)?S)?)?)$`)
Duration matches ISO 8601-1 duration regex TypeScript original code: export const duration: RegExp =
/^P(?:(\d+W)|(?!.*W)(?=\d|T\d)(\d+Y)?(\d+M)?(\d+D)?(T(?=\d)(\d+H)?(\d+M)?(\d+([.,]\d+)?S)?)?)$/;
Go equivalent (simplified without lookaheads): Matches either P<weeks>W or P<years>Y<months>M<days>D(T<hours>H<minutes>M<seconds>S)
var E164 = regexp.MustCompile(`^\+[1-9]\d{6,14}$`)
E164 matches E.164 international phone number standard Requires 7-15 digits total, first digit must be 1-9 (valid country codes don't start with 0) TypeScript Zod v4 code: /^\+[1-9]\d{6,14}$/
var Email = regexp.MustCompile(`^[A-Za-z0-9_'+\-]+([A-Za-z0-9_'+\-]*\.[A-Za-z0-9_'+\-]+)*@[A-Za-z0-9]([A-Za-z0-9\-]*[A-Za-z0-9])?(\.[A-Za-z0-9]([A-Za-z0-9\-]*[A-Za-z0-9])?)*\.[A-Za-z]{2,}$`)
Email provides practical email validation that rejects common invalid patterns This regex rejects: - IP addresses as domains (email@123.123.123.123) - Leading/trailing dots (.email@domain.com, email.@domain.com) - Consecutive dots (email..email@domain.com) - Domains without TLD (email@domain) - Invalid IP formats (email@111.222.333.44444) Uses Go-compatible syntax without negative lookahead
var Emoji = regexp.MustCompile(`^[` +
`\x{1F600}-\x{1F64F}` +
`\x{1F300}-\x{1F5FF}` +
`\x{1F680}-\x{1F6FF}` +
`\x{1F700}-\x{1F77F}` +
`\x{1F780}-\x{1F7FF}` +
`\x{1F800}-\x{1F8FF}` +
`\x{1F900}-\x{1F9FF}` +
`\x{1FA00}-\x{1FA6F}` +
`\x{1FA70}-\x{1FAFF}` +
`\x{2600}-\x{26FF}` +
`\x{2700}-\x{27BF}` +
`\x{1F1E6}-\x{1F1FF}` +
`\x{3000}-\x{303F}` +
`\x{3200}-\x{32FF}` +
`\x{1F004}` +
`\x{1F0CF}` +
`\x{1F18E}` +
`\x{1F191}-\x{1F19A}` +
`\x{1F201}` +
`\x{1F21A}` +
`\x{1F22F}` +
`\x{1F232}-\x{1F236}` +
`\x{1F238}-\x{1F23A}` +
`\x{1F250}` +
`\x{1F251}` +
`\x{1F3FB}-\x{1F3FF}` +
`\x{200D}` +
`\x{FE0F}` +
`]+$`)
Emoji matches emoji characters (Go-compatible implementation) Note: Go's regexp doesn't support Unicode properties like \p{Extended_Pictographic} This pattern covers comprehensive emoji ranges based on Unicode standards
var ExtendedDuration = regexp.MustCompile(`^[-+]?P(?:[-+]?\d+[.,]?\d*[YMWD])*(?:T(?:[-+]?\d+[.,]?\d*[HMS])*)?$`)
ExtendedDuration implements ISO 8601-2 extensions simplified for Go TypeScript original code: export const extendedDuration: RegExp =
/^[-+]?P(?!$)(?:(?:[-+]?\d+Y)|(?:[-+]?\d+[.,]\d+Y$))?(?:(?:[-+]?\d+M)|(?:[-+]?\d+[.,]\d+M$))?(?:(?:[-+]?\d+W)|(?:[-+]?\d+[.,]\d+W$))?(?:(?:[-+]?\d+D)|(?:[-+]?\d+[.,]\d+D$))?(?:T(?=[\d+-])(?:(?:[-+]?\d+H)|(?:[-+]?\d+[.,]\d+H$))?(?:(?:[-+]?\d+M)|(?:[-+]?\d+[.,]\d+M$))?(?:[-+]?\d+(?:[.,]\d+)?S)?)??$/;
Note: Go's regexp doesn't support lookaheads, so this is a simplified version
var GUID = regexp.MustCompile(`^[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}$`)
GUID matches any UUID-like identifier with 8-4-4-4-12 hex pattern TypeScript original code: export const guid: RegExp = /^([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})$/; Note: Using non-capturing group for performance (no backreferences needed)
var HTTPProtocol = regexp.MustCompile(`^https?$`)
HTTPProtocol matches http or https protocol Used by HttpURL schema to restrict to HTTP/HTTPS only TypeScript Zod v4: /^https?$/
var HTTPUrl = regexp.MustCompile(`^https?://[^\s/$.?#].[^\s]*$`)
HTTPUrl matches URLs starting with http or https specifically
var Hex = regexp.MustCompile(`^[0-9a-fA-F]*$`)
Hex matches hexadecimal strings (any length including empty) TypeScript Zod v4: /^[0-9a-fA-F]*$/
var Hostname = regexp.MustCompile(`^[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[-0-9a-zA-Z]{0,61}[0-9a-zA-Z])?)*\.?$`)
Hostname matches valid DNS hostnames according to RFC 1123 Based on Zod v4: /^(?=.{1,253}\.?$)[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[-0-9a-zA-Z]{0,61}[0-9a-zA-Z])?)*\.?$/ Note: Go's regex doesn't support lookahead, so length check (1-253 chars) must be done in validation code Rules: - Labels must start and end with alphanumeric characters - Labels can contain hyphens but not at start/end - Each label is 1-63 characters - Optional trailing dot allowed (FQDN format) - IP addresses ARE valid hostnames per Zod v4 tests
var Html5Email = regexp.MustCompile(`^[a-zA-Z0-9.!#$%&'*+/=?^_` + "`" + `{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$`)
Html5Email matches HTML5 input[type=email] validation implemented by browsers TypeScript original code: export const html5Email: RegExp =
/^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/;
var IP = regexp.MustCompile(`((?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9]))|((?:(?:[0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}|::|(?:[0-9a-fA-F]{1,4})?::(?:[0-9a-fA-F]{1,4}:?){0,6}))`)
IP matches any IP address (IPv4 or IPv6) TypeScript original code: export const ip: RegExp = new RegExp(`(${ipv4.source})|(${ipv6.source})`);
var IPv4 = regexp.MustCompile(`^(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])$`)
IPv4 matches IPv4 addresses TypeScript original code: export const ipv4: RegExp =
/^(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])$/;
var IPv6 = regexp.MustCompile(`^(([0-9a-fA-F]{1,4}:){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]))$`)
IPv6 matches IPv6 addresses – comprehensive pattern covering multiple compressed forms. TypeScript original code: export const ipv6: RegExp =
/^(([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}|::|([0-9a-fA-F]{1,4})?::([0-9a-fA-F]{1,4}:?){0,6})$/;
var Integer = regexp.MustCompile(`^-?\d+$`)
Integer matches integers (supports negative numbers) TypeScript original code (v4.1.11 fixed): export const integer: RegExp = /^-?\d+$/;
var JSONString = regexp.MustCompile(`^[\s\S]*$`)
JSONString matches any valid JSON string format Note: This is a simplistic pattern; actual validation should be done at runtime
var KSUID = regexp.MustCompile(`^[A-Za-z0-9]{27}$`)
KSUID matches strings in KSUID format TypeScript original code: export const ksuid: RegExp = /^[A-Za-z0-9]{27}$/;
var Lowercase = regexp.MustCompile(`^[^A-Z]*$`)
Lowercase matches strings with no uppercase letters TypeScript original code: export const lowercase: RegExp = /^[^A-Z]*$/;
var MACDefault = MAC(":")
MACDefault is the default MAC address regex using colon as delimiter
var MD5Hex = regexp.MustCompile(`^[0-9a-fA-F]{32}$`)
MD5 matches MD5 hash in hexadecimal format (32 hex chars)
var NanoID = regexp.MustCompile(`^[a-zA-Z0-9_-]{21}$`)
NanoID matches strings in NanoID format TypeScript original code: export const nanoid: RegExp = /^[a-zA-Z0-9_-]{21}$/;
var Null = regexp.MustCompile(`(?i)^null$`)
Null matches null values TypeScript original code: const _null: RegExp = /null/i; export { _null as null };
var Number = regexp.MustCompile(`^-?\d+(?:\.\d+)?$`)
Number matches numbers including decimals and negative numbers TypeScript original code (v4.1.11 fixed): export const number: RegExp = /^-?\d+(?:\.\d+)?$/;
var Rfc5322Email = regexp.MustCompile(`^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$`)
Rfc5322Email matches the classic emailregex.com regex for RFC 5322-compliant emails TypeScript original code: export const rfc5322Email =
/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
var SHA1Hex = regexp.MustCompile(`^[0-9a-fA-F]{40}$`)
SHA1 matches SHA-1 hash in hexadecimal format (40 hex chars)
var SHA256Hex = regexp.MustCompile(`^[0-9a-fA-F]{64}$`)
SHA256 matches SHA-256 hash in hexadecimal format (64 hex chars)
var SHA384Hex = regexp.MustCompile(`^[0-9a-fA-F]{96}$`)
SHA384 matches SHA-384 hash in hexadecimal format (96 hex chars)
var SHA512Hex = regexp.MustCompile(`^[0-9a-fA-F]{128}$`)
SHA512 matches SHA-512 hash in hexadecimal format (128 hex chars)
var String = regexp.MustCompile(`^[\s\S]*$`)
String matches any string with no length restrictions TypeScript original code:
export const string = (params?: { minimum?: number | undefined; maximum?: number | undefined }): RegExp => {
const regex = params ? `[\\s\\S]{${params?.minimum ?? 0},${params?.maximum ?? ""}}` : `[\\s\\S]*`;
return new RegExp(`^${regex}$`);
};
var ULID = regexp.MustCompile(`^[0-9A-HJKMNP-TV-Za-hjkmnp-tv-z]{26}$`)
ULID matches strings in ULID format TypeScript original code: export const ulid: RegExp = /^[0-9A-HJKMNP-TV-Za-hjkmnp-tv-z]{26}$/;
var URL = regexp.MustCompile(`^[a-zA-Z][a-zA-Z0-9+.-]*://[^\s/$.?#].[^\s]*$`)
URL matches valid URLs with proper format validation Supports common protocols: http, https, ftp, ftps, ws, wss, file, etc. Based on RFC 3986 with practical adaptations for common use cases
var UUID = regexp.MustCompile(`^(?:[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-8][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}|00000000-0000-0000-0000-000000000000)$`)
UUID matches RFC 4122 UUID with all versions supported when no version is specified TypeScript original code:
export const uuid = (version?: number | undefined): RegExp => {
if (!version)
return /^([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-8][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}|00000000-0000-0000-0000-000000000000)$/;
return new RegExp(
`^([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-${version}[0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12})$`
);
};
Note: Using non-capturing group for performance (no backreferences needed)
var UUID4 = regexp.MustCompile(`^[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}$`)
UUID4 matches version 4 UUIDs TypeScript original code: export const uuid4: RegExp = uuid(4); Note: Removed outer capturing group for performance (no backreferences needed)
var UUID6 = regexp.MustCompile(`^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-6[0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$`)
UUID6 matches version 6 UUIDs TypeScript original code: export const uuid6: RegExp = uuid(6); Note: Removed outer capturing group for performance (no backreferences needed)
var UUID7 = regexp.MustCompile(`^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-7[0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$`)
UUID7 matches version 7 UUIDs TypeScript original code: export const uuid7: RegExp = uuid(7); Note: Removed outer capturing group for performance (no backreferences needed)
var Undefined = regexp.MustCompile(`(?i)^undefined$`)
Undefined matches undefined values TypeScript original code: const _undefined: RegExp = /undefined/i; export { _undefined as undefined };
var UnicodeEmail = regexp.MustCompile(`^[^\s@"]{1,64}@[^\s@]{1,255}$`)
UnicodeEmail matches a loose regex that allows Unicode characters and enforces length limits TypeScript original code: export const unicodeEmail = /^[^\s@"]{1,64}@[^\s@]{1,255}$/u; Note: Go's regexp doesn't support full Unicode properties, so this regex is simplified
var Uppercase = regexp.MustCompile(`^[^a-z]*$`)
Uppercase matches strings with no lowercase letters TypeScript original code: export const uppercase: RegExp = /^[^a-z]*$/;
var XID = regexp.MustCompile(`^[0-9a-vA-V]{20}$`)
XID matches strings in XID format TypeScript original code: export const xid: RegExp = /^[0-9a-vA-V]{20}$/;
Functions ¶
func Datetime ¶
func Datetime(options DatetimeOptions) *regexp.Regexp
Datetime returns a regex for matching ISO 8601 datetime format TypeScript original code:
export function datetime(args: {
precision?: number | null;
offset?: boolean;
local?: boolean;
}): RegExp {
let regex = `${dateSource}T${timeSource(args)}`;
const opts: string[] = [];
opts.push(args.local ? `Z?` : `Z`);
if (args.offset) opts.push(`([+-]\\d{2}:?\\d{2})`);
regex = `${regex}(${opts.join("|")})`;
return new RegExp(`^${regex}$`);
}
func MAC ¶
MAC returns a regex for validating MAC addresses with the specified delimiter. Matches Zod TypeScript implementation using two branches for case-sensitive matching. TypeScript original code:
export const mac = (delimiter?: string): RegExp => {
const escapedDelim = util.escapeRegex(delimiter ?? ":");
return new RegExp(`^(?:[0-9A-F]{2}${escapedDelim}){5}[0-9A-F]{2}$|^(?:[0-9a-f]{2}${escapedDelim}){5}[0-9a-f]{2}$`);
};
The regex uses two alternatives: - First branch: uppercase hex digits (0-9A-F) - Second branch: lowercase hex digits (0-9a-f) This approach provides case-sensitive matching while accepting both cases.
Examples:
MAC(":") matches "00:1A:2B:3C:4D:5E" or "00:1a:2b:3c:4d:5e"
MAC("-") matches "00-1A-2B-3C-4D-5E" or "00-1a-2b-3c-4d-5e"
MAC(".") matches "00.1A.2B.3C.4D.5E" or "00.1a.2b.3c.4d.5e"
func StringRegex ¶
StringRegex returns a regex matching strings with optional min/max length limits
func Time ¶
func Time(opts TimeOptions) *regexp.Regexp
Time returns a regex for matching ISO 8601 time format TypeScript original code:
export function time(args: {
precision?: number | null;
}): RegExp {
return new RegExp(`^${timeSource(args)}$`);
}
func UUIDForVersion ¶
UUIDForVersion returns a regex matching a specific UUID version (1-8) TypeScript original code:
export const uuid = (version?: number | undefined): RegExp => {
if (!version)
return /^([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-8][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}|00000000-0000-0000-0000-000000000000)$/;
return new RegExp(
`^([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-${version}[0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12})$`
);
};
Types ¶
type DatetimeOptions ¶
type DatetimeOptions struct {
// Precision specifies number of decimal places for seconds
// If nil, matches any number of decimal places
// If 0 or negative, no decimal places allowed
Precision *int
// Offset if true, allows timezone offsets like +01:00
Offset bool
// Local if true, makes the 'Z' timezone marker optional
Local bool
}
DatetimeOptions defines parameters for datetime regex pattern TypeScript original code:
export function datetime(args: {
precision?: number | null;
offset?: boolean;
local?: boolean;
}): RegExp
type TimeOptions ¶
type TimeOptions struct {
// Precision specifies number of decimal places for seconds
// If nil, matches any number of decimal places
// If 0 or negative, no decimal places allowed
Precision *int
}
TimeOptions defines parameters for time regex pattern TypeScript original code:
function timeSource(args: { precision?: number | null }) {
let regex = `([01]\\d|2[0-3]):[0-5]\\d:[0-5]\\d`;
if (args.precision) {
regex = `${regex}\\.\\d{${args.precision}}`;
} else if (args.precision == null) {
regex = `${regex}(\\.\\d+)?`;
}
return regex;
}