Skip to content

Regex for Uuid V4 — Pattern, Examples & Caveats

Pattern flags: i
^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$

This pattern matches only version 4 (randomly generated) UUIDs: the version nibble is fixed to 4 and the variant nibble to 8, 9, a or b, with all other positions being hexadecimal. It is the strict form for validating random UUIDs. Identifier patterns recognize structured tokens such as UUIDs, hashes, Base64 strings and version numbers. These formats have exact layouts and alphabets defined by specifications, so the regex encodes group lengths, separators and allowed characters. A match confirms the shape; semantic validity (a real version, a correct checksum) usually needs an additional library check. It uses the i flag(s). Apply it to the whole input as a format contract. It rejects v1/v3/v5 UUIDs by design — use the broader uuid pattern if any version is acceptable. Hyphens are mandatory; strip them or use an alternative pattern for the compact 32-hex form. A matching string is well-formed but says nothing about whether it was generated by a cryptographically secure source. As with any regular expression, confirm its behavior in your target engine, test boundary and adversarial inputs, and watch for catastrophic backtracking before relying on it in production.

Advertisement
Ad slot · ref-top

Token-by-token explanation

  • [0-9a-f]{8} — eight hex digits
  • 4[0-9a-f]{3} — version fixed to 4, then three hex digits
  • [89ab][0-9a-f]{3} — variant nibble 8/9/a/b, then three hex digits
  • [0-9a-f]{12} — twelve hex digits
  • i flag — case-insensitive so uppercase hex also matches

What it matches

550e8400-e29b-41d4-a716-446655440000f47ac10b-58cc-4372-a567-0e02b2c3d479

What it does not match

123e4567-e89b-12d3-a456-426614174000550e8400-e29b-51d4-a716-446655440000550e8400e29b41d4a716446655440000

When to use it

Reach for an identifier pattern when you must recognize a standardized token — a UUID, a hash, a Base64 blob or a version string — before decoding or looking it up. The pattern confirms the layout and alphabet defined by the specification, which catches typos and wrong formats early. Follow it with a library that decodes or verifies semantics, since a correctly shaped identifier may still be invalid, expired or refer to nothing in your system.

Caveats

  • It rejects v1/v3/v5 UUIDs by design — use the broader uuid pattern if any version is acceptable.
  • Hyphens are mandatory; strip them or use an alternative pattern for the compact 32-hex form.
  • A matching string is well-formed but says nothing about whether it was generated by a cryptographically secure source.

Code examples

JavaScript
const re = /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
const input = "550e8400-e29b-41d4-a716-446655440000";
if (re.test(input)) {
  // valid: proceed
} else {
  // reject with a helpful message
}
Python
import re

rx = re.compile(r"^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$", re.I)
value = "550e8400-e29b-41d4-a716-446655440000"
if rx.fullmatch(value):
    pass  # valid
else:
    raise ValueError("invalid format")
Java
import java.util.regex.*;

Pattern p = Pattern.compile("^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$", Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher("550e8400-e29b-41d4-a716-446655440000");
boolean ok = m.matches();   // whole input must conform
PHP
<?php
$subject = "550e8400-e29b-41d4-a716-446655440000";
if (preg_match('/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i', $subject, $m)) {
    // $m[0] is the full match
}

How to test it

Before trusting any pattern, test it against three kinds of input: valid examples that must match, invalid examples that must be rejected, and edge cases at the boundaries (empty strings, maximum lengths, unusual separators and Unicode). Paste the expression into a live regex tester with your actual sample data and confirm the highlighted matches are exactly what you intend. Watch for catastrophic backtracking by trying long non-matching inputs, and remember that engine differences (JavaScript, Python, Java, PCRE, Go/RE2) mean a pattern that works in one may fail or behave differently in another — especially lookarounds, which Go's RE2 does not support at all.

Frequently asked questions

How is v4 different from other UUID versions?

Version 4 fills all non-fixed bits with random values, so the version nibble is always 4 and the variant is 8/9/a/b. Other versions derive bits from time, names or hashes.

Why fix the variant to [89ab]?

RFC 4122 sets the two most significant bits of the variant octet to 10, which makes the leading hex digit 8, 9, a or b. Enforcing it distinguishes real UUIDs from arbitrary hex.

Does a matching identifier guarantee it is real?

No — it confirms the textual shape only. A UUID-shaped string may not be a generated UUID, and a Base64-shaped string may decode to nonsense. Use a library to decode, verify checksums or confirm semantics.

What does the +json or +xml suffix mean?

It is the structured-syntax suffix convention (RFC 6839): application/geo+json is JSON with a defined structure. It tells clients the underlying serialization while naming the specific format.

Advertisement
Ad slot · ref-in-article

Try the Regex Tester

Live regex matching with groups, flags and explanations.

Open tool →