Definition
A UUID regex is a regular expression that matches the canonical string representation of a UUID: 32 hexadecimal characters arranged in five groups separated by hyphens, following the 8-4-4-4-12 pattern defined in RFC 9562.
The canonical format looks like:
xxxxxxxx-xxxx-Mxxx-Nxxx-xxxxxxxxxxxx
Where M is the version digit (1–8) and N is the variant digit (8, 9, a, or b for RFC 9562 UUIDs).
The standard UUID regex
Match any valid UUID (any version, any variant)
/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i
This is the most permissive form. It accepts all 32 hex characters with hyphens in the correct positions, regardless of version or variant. The i flag makes it case-insensitive — both a1B2... and A1b2... are valid.
Match only RFC 9562 variant UUIDs (versions 1–8)
/^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i
[1-8]at position 14 accepts only defined versions[89ab]at position 19 accepts only the RFC 4122 / RFC 9562 variant bits (10xx)
Match UUID v4 only
/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i
Match UUID v7 only
/^[0-9a-f]{8}-[0-9a-f]{4}-7[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i
Anatomy of the UUID string positions
Position: 0 9 14 19 36
| | | | |
Format: xxxxxxxx - xxxx - Mxxx - Nxxx - xxxxxxxxxxxx
Groups: [ 8 ] [4] [4] [4] [ 12 ]
| Position | Content | Regex constraint |
|---|---|---|
| 0–7 | 8 hex digits (high time bits in v1, random/timestamp in v7) | [0-9a-f]{8} |
| 9–12 | 4 hex digits | [0-9a-f]{4} |
| 14 | Version nibble (1–8) | [1-8] or specific digit |
| 15–17 | 3 hex digits | [0-9a-f]{3} |
| 19 | Variant nibble (8, 9, a, b) | [89ab] |
| 20–22 | 3 hex digits | [0-9a-f]{3} |
| 24–35 | 12 hex digits | [0-9a-f]{12} |
Usage by language
JavaScript / TypeScript
const UUID_REGEX = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
const UUID_V7_REGEX = /^[0-9a-f]{8}-[0-9a-f]{4}-7[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
function isUuid(value: string): boolean {
return UUID_REGEX.test(value);
}
function isUuidV7(value: string): boolean {
return UUID_V7_REGEX.test(value);
}
// Prefer the uuid library's validate() for application logic
import { validate, version } from 'uuid';
console.log(validate('018fbe3a-4c5d-7b12-8abc-0123456789ab')); // true
console.log(version('018fbe3a-4c5d-7b12-8abc-0123456789ab')); // 7
Python
import re
import uuid
UUID_REGEX = re.compile(
r'^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$',
re.IGNORECASE
)
def is_uuid(value: str) -> bool:
return bool(UUID_REGEX.match(value))
# For stricter validation, use the uuid module directly
def parse_uuid(value: str) -> uuid.UUID | None:
try:
return uuid.UUID(value)
except ValueError:
return None
u = parse_uuid('018fbe3a-4c5d-7b12-8abc-0123456789ab')
print(u.version) # 7
Go
import (
"regexp"
"github.com/google/uuid"
)
var uuidRegex = regexp.MustCompile(
`(?i)^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$`,
)
func IsUUID(s string) bool {
return uuidRegex.MatchString(s)
}
// Or use the library parser, which also checks version/variant
func ParseUUID(s string) (uuid.UUID, error) {
return uuid.Parse(s) // returns error for malformed strings
}
What regex cannot validate
A UUID regex checks format only. It cannot verify:
- Variant bits are correct — a string with
cat position 19 passes the permissive regex but is not an RFC 9562 UUID - Version digit is a defined version —
9at position 14 passes[0-9a-f]but no UUID version 9 exists - Randomness quality — a UUID with all zeros except the version/variant bits is syntactically valid but not randomly generated
For application logic that cares about version and variant, use a dedicated UUID library. For boundary validation (HTTP request body, database input sanitisation), the permissive regex is appropriate and fast.
The nil UUID and regex
The nil UUID (00000000-0000-0000-0000-000000000000) and the Max UUID (ffffffff-ffff-ffff-ffff-ffffffffffff) both pass the permissive regex. If you need to reject them:
const NON_NIL_UUID_REGEX =
/^(?!00000000-0000-0000-0000-000000000000)[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
Related terms
- UUID String Format — the canonical 36-character representation
- Variant — the bits at position 19 that distinguish RFC 9562 UUIDs
- Nil UUID — the all-zeros special value
- RFC 9562 — the specification defining the UUID format
Validate UUIDs with the UUID v4 generator or UUID v7 generator.
Frequently asked questions
What is the correct regex to validate any UUID?
The standard case-insensitive pattern is /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i. This matches all 128 bits encoded as 32 hex digits with hyphens at positions 8, 12, 16, and 20. It accepts any version (1–8) and any variant. Use the i flag to accept both upper and lowercase hex digits, since both are valid per RFC 9562.
How do I match only UUID v4 or UUID v7 with a regex?
For UUID v4 specifically: /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i — the 4 at position 13 locks the version digit and [89ab] at position 17 locks the RFC 9562 variant. For UUID v7: replace the 4 with 7: /^[0-9a-f]{8}-[0-9a-f]{4}-7[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i.
Should I use regex or a UUID library to validate UUIDs?
Use a library for application logic; use regex for format checks at the boundary (HTTP input validation, database column checks, CLI argument parsing). Libraries such as the uuid npm package provide validate() and version() functions that parse the full structure including version and variant bits. Regex alone cannot verify that the variant bits are correctly set — it can only check the character pattern.