Regex for UUID Validation — v1, v3, v4, v5, and v7
Regex for UUID validation per RFC 4122 and RFC 9562. Generic, version-specific (v4, v7), and case-insensitive patterns with tested examples.
Detailed Explanation
Regex for UUID Validation
A UUID is a 128-bit identifier formatted as 8-4-4-4-12 hexadecimal digits separated by hyphens. RFC 4122 (and the updated RFC 9562 for v6–v8) specifies the version digit (13th nibble) and variant bits (17th nibble).
Generic UUID Pattern
^[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}$
This matches any UUID-shaped string regardless of version.
Strict RFC-Compliant Pattern
^[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}$
The third group starts with the version digit (1–8), and the fourth group starts with 8, 9, a, or b (variant bits 10xx).
Version-Specific Patterns
| Version | Pattern (third group prefix) |
|---|---|
| v1 (timestamp) | [0-9a-fA-F]{8}-[0-9a-fA-F]{4}-1[0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12} |
| v4 (random) | [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} |
| v7 (time-ordered) | [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} |
Tested Examples
| Input | Generic | Strict |
|---|---|---|
f47ac10b-58cc-4372-a567-0e02b2c3d479 |
yes | yes (v4) |
550e8400-e29b-41d4-a716-446655440000 |
yes | yes (v1) |
00000000-0000-0000-0000-000000000000 |
yes | no (nil UUID) |
f47ac10b58cc4372a5670e02b2c3d479 |
no | no (no hyphens) |
g47ac10b-58cc-4372-a567-0e02b2c3d479 |
no | no (g is not hex) |
The Nil UUID
00000000-0000-0000-0000-000000000000 is reserved by RFC 4122 and is technically not a valid generated UUID. The strict pattern above rejects it because the version digit is 0.
Trim Curly Braces
Some systems (Windows GUIDs, .NET) wrap UUIDs in { }. Strip them before matching, or extend the pattern: ^\{?…\}?$.
Use Case
Validating UUIDs received in URL parameters before performing a database lookup, distinguishing v4 from v7 IDs in a logging pipeline, or filtering out the nil UUID from user-supplied identifiers.