DevRivet

Blog ·

Validate a UUID String with a Regex: Format and Version Check

Use this regex to validate a UUID string's 8-4-4-4-12 format, with optional version and variant checks. Includes JavaScript and Python examples and common pitfalls.

UUID Generator

Generate secure UUID v4 and time-ordered UUID v7 identifiers.

Try it free →

To validate a UUID string with a regex, check the 8-4-4-4-12 hexadecimal layout. This pattern matches any well-formed UUID string: /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i. To also check the RFC 4122 version and variant bits, use the stricter pattern shown below.

If you want to generate a UUID to test your validation, the UUID Generator can produce v4 and v7 values, and its validate mode checks a pasted string against the same rules. Use it to confirm your regex accepts what you expect.

The basic format regex

A UUID is 32 hexadecimal digits split into five groups with hyphens: 8, 4, 4, 4 and 12 characters. The regex needs to match exactly that shape and nothing else.

const UUID_FORMAT = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;

UUID_FORMAT.test('550e8400-e29b-41d4-a716-446655440000'); // true
UUID_FORMAT.test('550E8400-E29B-41D4-A716-446655440000'); // true (case-insensitive)
UUID_FORMAT.test('550e8400e29b41d4a716446655440000');      // false (no hyphens)
UUID_FORMAT.test('{550e8400-e29b-41d4-a716-446655440000}'); // false (braces)

The anchors ^ and $ matter. Without them, the pattern matches a UUID found anywhere inside a longer string, such as a path segment with extra characters around it. The i flag accepts uppercase hex, which is correct because UUIDs are case-insensitive. If your system stores UUIDs in one case, normalize with toLowerCase() before comparing.

Checking the version and variant bits

RFC 4122 and its successor RFC 9562 put two fields inside the UUID that a format check ignores. The first hex digit of the third group is the version. The first hex digit of the fourth group is the variant, and for RFC UUIDs it must be 8, 9, a or b.

This stricter pattern accepts versions 1 through 8 and the RFC variant:

const UUID_STRICT = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;

UUID_STRICT.test('550e8400-e29b-41d4-a716-446655440000'); // true (version 4, variant a)
UUID_STRICT.test('550e8400-e29b-01d4-a716-446655440000'); // false (version 0)

The second string has the right shape but its version nibble is 0. That is not a defined UUID version, so the strict pattern rejects it while the format pattern accepts it. This is the case a basic check misses.

Two special values need a decision from you. The nil UUID 00000000-0000-0000-0000-000000000000 and the max UUID ffffffff-ffff-ffff-ffff-ffffffffffff both pass the format check. The strict pattern rejects both because their version nibble is 0 or f. Some systems use the nil UUID as a sentinel, so allow it explicitly if you need to:

function isValidUuid(value, { allowNil = false } = {}) {
  if (allowNil && value === '00000000-0000-0000-0000-000000000000') return true;
  return UUID_STRICT.test(value);
}

Python

In Python, use re.fullmatch so the pattern must cover the whole string. Plain re.match only anchors the start, and $ also matches just before a trailing newline, which can let a bad value through.

import re

UUID_STRICT = 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,
)

def is_valid_uuid(value: str) -> bool:
    return UUID_STRICT.fullmatch(value) is not None

print(is_valid_uuid('550e8400-e29b-41d4-a716-446655440000'))  # True
print(is_valid_uuid('550e8400-e29b-01d4-a716-446655440000'))  # False
print(is_valid_uuid('550e8400-e29b-41d4-a716-446655440000\n'))  # False

If you only need to know whether a string is a UUID and do not care about the version, Python's standard library offers uuid.UUID(value), which raises ValueError for malformed input. It accepts several input forms, though, so read the next section before relying on it for strict validation.

Format validation vs. proving a UUID was generated

A regex answers one question: does this string look like a UUID? It cannot tell you whether a real generator produced it. Anyone can type a string that passes the pattern, and a passing check does not mean the value is unique, random or issued by your system.

  • Format validation: the string has the right shape, hex characters and, with the strict pattern, a valid version and variant. This is cheap and safe to run on any input.
  • Generation check: the value came from a trusted source, such as your database or an authenticated API. Validation cannot establish this from the string alone.
  • Structural check for v7: UUID v7 starts with a 48-bit Unix millisecond timestamp, so you can decode that part and check that it falls in a plausible range. See UUID v4 vs v7 for the layout.
  • No structural check for v4: a v4 UUID carries 122 random bits and no recognizable pattern beyond the version and variant fields. If a v4 value passes the strict regex, that is all you can learn from the string.

In practice, validate UUIDs at the boundary of your system, such as a request parameter or a file import, to reject malformed input early. Then rely on your database's primary key or unique constraint to enforce uniqueness, and on your own generation code to produce the value. Do not treat a regex match as authorization or proof of origin.

Common pitfalls

  • Forgetting the anchors. Without ^ and $, a longer string containing a UUID will pass.
  • Using the format pattern when you need the version check. The version digit is easy to get wrong in hand-written test data.
  • Rejecting uppercase. RFC UUIDs are case-insensitive, so use the i flag or lowercase the input first.
  • Accepting braces or the urn:uuid: prefix without meaning to. These forms are sometimes used in Windows and COM contexts. Strip them first if you need to accept them, and document it.
  • Using $ in Python without fullmatch. A trailing newline can pass a pattern that looks correct. Use re.fullmatch or \Z.
  • Assuming a valid pattern means a valid record. Check existence in your database when that matters.

For background on why UUIDs are unlikely to collide in practice, see Are UUIDs Really Unique?. This post focuses on checking the string itself.

Quick reference

  • Format only: ^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$ with case-insensitive matching.
  • Strict RFC: third group starts with [1-8], fourth group starts with [89ab].
  • Nil and max UUIDs fail the strict check. Allow them explicitly if your data uses them.
  • Python: use re.fullmatch, not re.match with a trailing $.
  • A passing regex proves shape, not origin.

FAQ

Should I use the strict or the format-only regex?

Use the strict pattern if you accept only RFC-compliant UUIDs, which is the usual case for new systems. Use the format-only pattern if you need to accept legacy or non-standard values that use other version digits.

Does a regex check whether a UUID is unique?

No. A regex only checks the string's shape. Uniqueness is enforced by a unique index or constraint in your database.

Can I validate a UUID in SQL?

Many databases have a native UUID type that rejects malformed values on insert. If you store UUIDs as text, you can use your database's regex operator, but the exact syntax differs by engine, so test it against your version.

Do I need to validate UUIDs I generate myself?

Usually not. Values from a well-tested library already match the format. Validation is most useful for input from users, clients and external systems.

UUID Generator

Generate secure UUID v4 and time-ordered UUID v7 identifiers.

Try it free →