Regex for UUID Validation
A UUID is 32 hexadecimal digits in an 8-4-4-4-12 grouping: five hyphen-separated blocks. This pattern validates that shape for any UUID version; the variation below pins the version nibble to 4 and the variant bits, which is what you want when your system only ever generates v4.
/^[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}$/How it works, token by token
| Token | Meaning |
|---|---|
| ^[0-9a-fA-F]{8} | first block: exactly 8 hex digits |
| -[0-9a-fA-F]{4} | second block: 4 hex digits (repeated for blocks three and four) |
| -[0-9a-fA-F]{12}$ | final block: 12 hex digits, then end of string |
What it matches
f47ac10b-58cc-4372-a567-0e02b2c3d479F47AC10B-58CC-4372-A567-0E02B2C3D47900000000-0000-0000-0000-000000000000f47ac10b58cc4372a5670e02b2c3d479f47ac10b-58cc-4372-a567g47ac10b-58cc-4372-a567-0e02b2c3d479Try it live
One candidate per line — the m flag makes the ^ and $ anchors apply to each line. Edit anything; it runs in your browser.
Variations
^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$strict v4: version nibble is 4, variant nibble is 8, 9, a, or b — add the i flag for uppercase^[0-9A-HJKMNP-TV-Z]{26}$ULID instead: 26 chars of Crockford Base32 (no I, L, O, U)Language notes
- Add the i flag (or re.IGNORECASE) and you can shorten the classes to [0-9a-f].
- Most languages ship a real parser — uuid.UUID(s) in Python, uuid.Parse(s) in Go — which also normalizes case and validates versions.
Frequently asked questions
Should UUIDs be matched case-insensitively?
Yes — RFC 4122 outputs lowercase but requires parsers to accept uppercase. This pattern includes both ranges; with the i flag you can write it more compactly.
How do I match only version 4 UUIDs?
Pin the two structural nibbles: the third block must start with 4 (the version) and the fourth with 8, 9, a, or b (the variant). The strict variation above does exactly that.
Building something custom? The regex tester gives you live match highlighting for any pattern.