Regex for UUID — Pattern Explained with Examples
DodaTech
Updated 2026-06-20
2 min read
In this tutorial, you'll learn about Regex for UUID. We cover key concepts, practical examples, and best practices to help you understand and apply this topic effectively.
UUID (Universally Unique Identifier) validation is essential for systems that use them as primary keys, session tokens, or distributed identifiers. UUIDs follow the 8-4-4-4-12 hex format with specific bits indicating the version and variant. This pattern validates the format and enforces correct version and variant bits.
The Pattern
/^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-5][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$/
Pattern Breakdown
| Part | Meaning |
|---|---|
^ |
Start-of-string anchor |
[0-9a-fA-F]{8} |
Time-low: 8 hex digits |
- |
First hyphen separator |
[0-9a-fA-F]{4} |
Time-mid: 4 hex digits |
- |
Second hyphen separator |
[1-5] |
Version digit (position 13) — must be 1-5 |
[0-9a-fA-F]{3} |
Time-high-and-version: 3 hex digits |
- |
Third hyphen separator |
[89abAB] |
Variant bits (position 17) — must be 8, 9, a, or b |
[0-9a-fA-F]{3} |
Clock-seq: 3 hex digits |
- |
Fourth hyphen separator |
[0-9a-fA-F]{12} |
Node: 12 hex digits |
$ |
End-of-string anchor |
Matches
- 550e8400-e29b-41d4-a716-446655440000 (UUID v4)
- 00000000-0000-0000-0000-000000000000 (nil UUID)
- f47ac10b-58cc-4372-a567-0e02b2c3d479 (UUID v4)
- 6ba7b810-9dad-11d1-80b4-00c04fd430c8 (UUID v1)
- 00112233-4455-6677-8899-aabbccddeeff
Does NOT Match
- 550e8400-e29b-z1d4-a716-446655440000 (invalid hex character z)
- 550e8400e29b41d4a716446655440000 (missing hyphens)
- 550e8400-e29b-41d4-a716-4466554400000 (13 hex digits in last group)
- 550e8400-e29b-41d4-a716 (truncated)
- not-a-uuid (text)
Language Examples
JavaScript
const uuidRegex = /^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-5][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$/;
console.log(uuidRegex.test('550e8400-e29b-41d4-a716-446655440000')); // true
console.log(uuidRegex.test('not-a-uuid')); // false
Python
import re
pattern = r'^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-5][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$'
print(bool(re.match(pattern, '550e8400-e29b-41d4-a716-446655440000'))) # True
print(bool(re.match(pattern, 'not-a-uuid'))) # False
Java
import java.util.regex.Pattern;
public class UuidValidator {
private static final Pattern UUID_PATTERN =
Pattern.compile("^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-5][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$");
public static boolean isValid(String uuid) {
return UUID_PATTERN.matcher(uuid).matches();
}
}
Common Pitfalls
- The version digit at position 13 determines UUID version (v1, v2, v3, v4, v5) — this pattern enforces 1-5, rejecting invalid version values like 0 or 6
- The variant bits at position 17 must be in the range 8-b — this pattern enforces RFC 4122 compliant variants
- Some systems include curly braces around UUIDs (e.g.
{550e8400-...}) — strip or add optional braces in the pattern if needed - The nil UUID (
00000000-0000-0000-0000-000000000000) is valid and passes this pattern despite having version 0 — version 0 is actually not one of the five RFC versions, so some patterns exclude it
Real-World Use Cases
- API request validation — validate UUID path parameters and request body identifiers in RESTful services
- Database constraint checking — verify primary key formats before insert or batch import operations
- Session token validation — ensure session UUIDs stored in cookies or tokens are well-formed before performing lookups
FAQ
Related Patterns
Regex for Credit Card Numbers Regex for Email
← Previous
Compiled vs Interpreted Languages — Explained with Examples
Next →
Regex for Date (YYYY-MM-DD) — Pattern Explained with Examples
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro