Regex for UUIDs
A regular expression for UUIDs in their usual 8-4-4-4-12 form that also checks the version and variant digits defined in RFC 9562, with looser and stricter variants.
This tool runs in your browser. We never send what you type or paste to a server.
The pattern
/^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/iRFC 9562, which replaced RFC 4122 in 2024, writes a UUID as 32 hexadecimal digits in groups of 8, 4, 4, 4 and 12, and says the letters may be uppercase, lowercase or mixed. Two of the digits are not random: the first digit of the third group is the version, and the first digit of the fourth group holds the variant. This pattern checks both.
The tester at the top of this page is loaded with this pattern and the examples below, one per line. It adds the m flag so that ^ and $ apply to each line, and g to show every match. Edit either box to try your own input.
How it works, token by token
| Part | Meaning |
|---|---|
^ | Start of the string. |
[0-9a-f]{8} | Eight hex digits. The i flag allows capitals. |
- | Hyphen. |
[0-9a-f]{4} | Four hex digits. |
- | Hyphen. |
[1-8] | The version digit: 1 to 8 are the versions RFC 9562 defines. 4 is random, 7 is time-ordered. |
[0-9a-f]{3} | Three more hex digits. |
- | Hyphen. |
[89ab] | The variant: the top two bits of this digit are binary 10 for RFC 9562 UUIDs, which makes it 8, 9, a or b. |
[0-9a-f]{3} | Three more hex digits. |
- | Hyphen. |
[0-9a-f]{12} | The last twelve hex digits. |
$ | End of the string. |
What it matches and what it rejects
| Input | Result | Why |
|---|---|---|
f81d4fae-7dec-11d0-a765-00a0c91e6bf6 | matches | Version 1, the example in RFC 9562. |
919108f7-52d1-4320-9bac-f847db4148a8 | matches | Version 4, from the RFC's test vectors. |
2ed6657d-e927-568b-95e1-2665a8aea6a2 | matches | Version 5. |
017F22E2-79B0-7CC3-98C4-DC0C0C07398F | matches | Version 7, in capitals. |
00000000-0000-0000-0000-000000000000 | rejected | The nil UUID has version 0. |
FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF | rejected | The max UUID has version f. |
919108f7-52d1-3320-5bac-f847db4148a8 | rejected | Variant digit 5: not an RFC 9562 UUID. |
919108f752d143209bacf847db4148a8 | rejected | No hyphens. |
{919108f7-52d1-4320-9bac-f847db4148a8} | rejected | Braces, as Microsoft tools often write them. |
919108f7-52d1-4320-9bac-f847db4148a | rejected | One digit short. |
Looser and stricter versions
If you only need to know that the string has the right shape, for example because the IDs come from a system that does not follow the RFC, or you want to allow the nil and max UUIDs, drop the version and variant checks:
/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i| Input | Result | Why |
|---|---|---|
919108f7-52d1-4320-9bac-f847db4148a8 | matches | Version 4. |
00000000-0000-0000-0000-000000000000 | matches | The nil UUID. |
FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF | matches | The max UUID. |
919108f752d143209bacf847db4148a8 | rejected | Still needs the hyphens. |
919108f7-52d1-4320-9bac-f847db4148ag | rejected | g is not hex. |
If your system only creates random version 4 UUIDs, you can insist on that version, which catches IDs pasted from the wrong place:
/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i| Input | Result | Why |
|---|---|---|
919108f7-52d1-4320-9bac-f847db4148a8 | matches | Version 4. |
017F22E2-79B0-7CC3-98C4-DC0C0C07398F | rejected | Version 7. |
f81d4fae-7dec-11d0-a765-00a0c91e6bf6 | rejected | Version 1. |
RFC 9562 defines the nil UUID (all zeros, section 5.9) and the max UUID (all f, section 5.10) as special values. They are sometimes used to mean "none" or "all", which is why the any-hex pattern accepts them and the strict one does not.
Other spellings of the same UUID
The same 128-bit value turns up as {919108f7-…} in braces from Windows tools, as urn:uuid:919108f7-… in XML, and as 32 digits with no hyphens in some databases. Python's uuid.UUID accepts all of these and gives back the canonical form, and so do most UUID libraries. If you accept several spellings, convert to one form, lowercase with hyphens, before storing or comparing, so the same ID is never stored twice.
Databases that have a UUID type, such as PostgreSQL, store the 16 bytes rather than the text and do the conversion for you, which also makes indexes smaller. Time-ordered version 7 UUIDs keep new rows near each other in an index, which is why RFC 9562 recommends them over version 4 for database keys. You can generate both kinds with the UUID generator.
Finding UUIDs in text
To find UUIDs in log lines, drop the anchors and use \b on each side, with the g flag to find them all and i for capitals: \b[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\b. The word boundaries stop a match from starting or ending inside a longer run of hex digits.
In JavaScript, Python, PHP and C#
JavaScript
const re = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
console.log(re.test('f81d4fae-7dec-11d0-a765-00a0c91e6bf6')); // → true
console.log(re.test('00000000-0000-0000-0000-000000000000')); // → false
Python
import re
pattern = re.compile(r'[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}', re.IGNORECASE)
print(bool(pattern.fullmatch('f81d4fae-7dec-11d0-a765-00a0c91e6bf6'))) # → True
print(bool(pattern.fullmatch('00000000-0000-0000-0000-000000000000'))) # → False
PHP
<?php
$pattern = '/^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/iD';
echo preg_match($pattern, 'f81d4fae-7dec-11d0-a765-00a0c91e6bf6'), "\n"; // → 1
echo preg_match($pattern, '00000000-0000-0000-0000-000000000000'), "\n"; // → 0
C#
using System;
using System.Text.RegularExpressions;
var pattern = new Regex(@"^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}\z", RegexOptions.IgnoreCase);
Console.WriteLine(pattern.IsMatch("f81d4fae-7dec-11d0-a765-00a0c91e6bf6")); // → True
Console.WriteLine(pattern.IsMatch("00000000-0000-0000-0000-000000000000")); // → FalseEach version checks the whole string. JavaScript's $ only matches at the very end, but Python, PHP and .NET also let $ match just before a final line break, so "abc\n" could slip through. The Python version uses re.fullmatch (so the ^ and $ are dropped), PHP adds the D modifier, and C# ends with \z.
The JavaScript, Python and C# examples are run before each update and their output is compared with the comments. PHP is not installed on our build machine, so the PHP version is checked against the PCRE pattern modifier documentation, and its string literal is checked to hand PHP exactly the pattern tested here.
Questions
What is the regex for a UUID?
^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$ with the i flag. It checks the 8-4-4-4-12 shape plus the version and variant digits from RFC 9562.
Are UUIDs case-sensitive?
No. RFC 9562 says the hex letters may be uppercase, lowercase or mixed. Lowercase is the usual choice when storing them.
How can I tell which version a UUID is?
Look at the first character of the third group: in 919108f7-52d1-4320-9bac-f847db4148a8 it is 4, so it is a random version 4 UUID.