Regex for UUID

Use /^[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}$/ to check that a whole string is a valid for UUID. Every example below is tested against the pattern, and you can open it in the Regex Tester with the samples preloaded.

Updated

/^[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}$/
Open in Regex Tester
InputResult
123e4567-e89b-12d3-a456-426614174000✓ Match
550e8400-e29b-41d4-a716-446655440000✓ Match
01890a5d-ac96-774b-bcce-b302099a8057✓ Match
123e4567e89b12d3a456426614174000✗ No match
123e4567-e89b-02d3-a456-426614174000✗ No match
zzzzzzzz-e89b-12d3-a456-426614174000✗ No match

How the pattern works

PartMeaning
[0-9a-fA-F]{8}Eight hex digits
[1-8][0-9a-fA-F]{3}The third group starts with the version, 1–8
[89abAB][0-9a-fA-F]{3}The fourth group starts with the RFC 9562 variant: 8, 9, a or b
[0-9a-fA-F]{12}Twelve hex digits at the end
-Dashes in the 8-4-4-4-12 positions

Code examples

JavaScript

const re = /^[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}$/;
re.test(value); // true or false

Python

import re

bool(re.fullmatch(r'[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}', value))

PHP

preg_match('/^[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}$/', $value) === 1;

Notes and limitations

To accept only version 4 (random) UUIDs, change [1-8] to 4. Generate test values with the UUID Generator.

Regex TesterType a pattern and watch matches highlight live in your test text.
Open the tool

Frequently asked questions

What is the regex for UUID?
/^[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}$/ — it matches the whole string. See the table above for what each part does.
Which examples match?
Of the examples tested, these match: 123e4567-e89b-12d3-a456-426614174000, 550e8400-e29b-41d4-a716-446655440000, 01890a5d-ac96-774b-bcce-b302099a8057. The others don’t.
How do I test this regex?
Click “Open in Regex Tester” — the pattern, flags and sample text load automatically, and matches highlight as you edit.