Quick answer
A UUID regex is ^[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 layout of 32 hex digits, that the version digit (first of the third group) is 1 to 8, and that the variant digit (first of the fourth group) is 8, 9, a or b as RFC 9562 requires. Use [4] or [7] in place of [1-8] to accept one version only.
- Tested examples
- Token-by-token breakdown
- Code in 5 languages
- Live tester
How to use the UUID regex
- 1Copy the pattern with Copy pattern in the tester, or take a ready-made snippet for your language from the code section below.
- 2Check the sample lines in Test string: every line the UUID regex accepts is highlighted, rejected lines stay plain.
- 3Paste your own values one per line to see which pass, and read Explanation for what each part of the pattern does.
- 4Open the Code tab for JavaScript, Python, Go, C#, PHP or Dart with the pattern translated and escaped.
- 5Validate again on the server: client-side checks give users quick feedback, not security.
How does the UUID regex work?
Read it left to right. Each piece below matches one part of the input:
^[0-9a-f]{8}8 hex digits. With the i flag, A–F are accepted too.-[0-9a-f]{4}A hyphen and 4 hex digits.-[1-8][0-9a-f]{3}The version digit (1–8) and 3 more hex digits. Version 4 is random, version 7 is time-ordered.-[89ab][0-9a-f]{3}The variant: the first digit is 8, 9, a or b, the RFC 9562 layout.-[0-9a-f]{12}$12 hex digits and the end of the string.
Flags: i (ignore case).
Which strings does the UUID regex match?
The whole input must match, from the first character to the last. Every row below is checked against the pattern by our automated test suite before each release.
| Input | Matches? | Why |
|---|---|---|
550e8400-e29b-41d4-a716-446655440000 | Yes | Version 4 (random). |
01890a5d-ac96-774b-bcce-b302099a8057 | Yes | Version 7 (time-ordered). |
123e4567-e89b-12d3-a456-426614174000 | Yes | Version 1 (time and node). |
F47AC10B-58CC-4372-A567-0E02B2C3D479 | Yes | Uppercase hex, thanks to the i flag. |
550e8400e29b41d4a716446655440000 | No | No hyphens (see the variant below). |
550e8400-e29b-41d4-a716-44665544000 | No | The last group has only 11 digits. |
550e8400-e29b-41d4-c716-446655440000 | No | Variant c is the legacy Microsoft layout, not RFC 9562. |
550e8400-e29b-91d4-a716-446655440000 | No | There is no version 9. |
00000000-0000-0000-0000-000000000000 | No | The nil UUID has version 0. Check for it separately if you allow it. |
{550e8400-e29b-41d4-a716-446655440000} | No | Braces, as Windows registry tools print them. |
How do I match only UUID v4 or v7?
Replace the version class:
| Accept | Third group starts with |
|---|---|
| Version 4 only | 4 |
| Version 7 only | 7 |
| Versions 4 and 7 | [47] |
| Any of 1–8 (this page) | [1-8] |
Version 7 (RFC 9562, 2024) puts a millisecond timestamp in the first 48 bits, so v7 IDs sort by creation time and index well in databases. Version 4 is 122 random bits. To generate either, use the UUID generator; for an even more compact time-sortable ID, see ULID.
What about the nil UUID, max UUID and other spellings?
RFC 9562 defines two special values that this pattern rejects because their version digit is not 1–8: the nil UUID 00000000-0000-0000-0000-000000000000 and the max UUID ffffffff-ffff-ffff-ffff-ffffffffffff. Allow them explicitly if they are meaningful in your system.
Other spellings you may meet:
- No hyphens (
550e8400e29b41d4a716446655440000):^[0-9a-f]{12}[1-8][0-9a-f]{3}[89ab][0-9a-f]{15}$. - Braces or a urn:uuid: prefix: strip them before validating.
- Uppercase: valid input, but RFC 9562 says to output lowercase, so normalize before storing to keep comparisons exact.
How do I use the UUID regex in JavaScript, Python, Go, C# and PHP?
The pattern is translated for each engine rather than pasted as is: named groups, anchors, escapes, shorthand classes and flags are adjusted so each snippet behaves as closely to the JavaScript version as its engine allows. Differences that remain (Go's RE2 has no lookarounds or backreferences, for example) are called out under each one. The Code tab in the tester above also exports Dart, and updates as you edit the pattern.
JavaScript and TypeScript
const regex = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
function isValid(value) {
return regex.test(value);
}
console.log(isValid("550e8400-e29b-41d4-a716-446655440000"));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}\Z", re.IGNORECASE)
def is_valid(value: str) -> bool:
return pattern.search(value) is not None
print(is_valid("550e8400-e29b-41d4-a716-446655440000"))- JavaScript's $ only matches at the very end of the string. Python's $ also matches before a trailing newline, so the pattern uses \Z instead.
Go
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`(?i)^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$`)
fmt.Println(re.MatchString("550e8400-e29b-41d4-a716-446655440000"))
}C#
using System;
using System.Text.RegularExpressions;
// The timeout stops a pathological input from hanging the thread (ReDoS).
var regex = 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, TimeSpan.FromSeconds(1));
Console.WriteLine(regex.IsMatch("550e8400-e29b-41d4-a716-446655440000"));- JavaScript's $ only matches at the very end of the string. .NET's $ also matches before a trailing newline, so the pattern uses \z instead.
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';
function isValid(string $value, string $pattern): bool
{
return preg_match($pattern, $value) === 1;
}
var_dump(isValid('550e8400-e29b-41d4-a716-446655440000', $pattern));- The D modifier makes $ match only at the very end, like JavaScript (PCRE's $ also matches before a trailing newline).
Which related regex patterns are useful?
- Regex for Semantic Versioning (SemVer): major.minor.patch with optional prerelease and build metadata, per semver.org.
- Regex for JWT (JSON Web Token) Format: Three base64url segments separated by dots: header, payload and an optional signature.
- Regex for Hex Color Codes: CSS hex colors in 3, 4, 6 or 8 digits with a leading hash.
- Regex for MAC Addresses: Six hex pairs with consistent : or - separators, or Cisco's dotted form.
Browse all 25 patterns in the regex pattern library, or build your own in the regex tester.
Frequently asked questions
Last updated by the Playxoft engineering team.