You need to check a UUID from a form field or URL before sending it to the database.
The regex below checks the text format, version and variant. Compare it with your language's parser before choosing which checks your application needs.
The regex
^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$
Use it with the case insensitive flag so that uppercase input passes. Piece by piece:
[0-9a-f]{8}, then a hyphen: the first group is eight hex digits.[0-9a-f]{4}: the second group is four hex digits.[1-8][0-9a-f]{3}: the third group starts with the version digit, which must be 1 through 8 for a standard UUID.[89ab][0-9a-f]{3}: the fourth group starts with the variant, which for RFC UUIDs is 8, 9, a or b.[0-9a-f]{12}: the last group is twelve hex digits.^and$anchor the whole string so that "abc" plus a UUID plus "xyz" does not match.
Common mistakes
- Forgetting the anchors. Without
^and$the pattern matches a UUID anywhere inside a longer string. - Allowing any hex digit for version and variant. A pattern of five plain hex groups accepts strings that are not valid UUIDs. Whether you care depends on what you do next.
- Hard coding version 4. Many copied patterns use
4[0-9a-f]{3}for the third group. That rejects every v1 and v7 UUID. Only do this if you truly require v4. - Using
\wor.instead of hex classes. Both accept characters that cannot appear in a UUID.
Variants you may need
Only accept version 7:
^[0-9a-f]{8}-[0-9a-f]{4}-7[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$
Accept the nil UUID too, since it fails the version check above:
^(?:[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}|00000000-0000-0000-0000-000000000000)$
Accept optional braces in the Windows GUID style:
^(?:[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}|\{[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}\})$
The alternatives accept a bare UUID or a pair of braces. A single brace fails. In Python, use fullmatch so trailing line breaks fail too.
Ready to use snippets
JavaScript:
const UUID_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;
const isUuid = (s) => UUID_RE.test(s);
Python:
import re
UUID_RE = 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.I)
def is_uuid(s): return bool(UUID_RE.fullmatch(s))
The better option: parse it
A parser can turn text into a UUID value. Its accepted spellings and bit checks depend on the language or library.
The examples below include parsing functions and the uuid package's validation function. Check their rules against the format, version and variant your application requires.
# Python
import uuid
try:
uuid.UUID(s)
except ValueError:
... # not a UUID
// JavaScript with the uuid package
import { validate, version } from "uuid";
validate(s); // true or false
version(s); // 4, 7, ...
// Java
UUID.fromString(s); // throws IllegalArgumentException
// C#
Guid.TryParse(s, out var g);
Python's uuid.UUID parser accepts braces, the urn:uuid: prefix and text without hyphens.
A length check does not enforce the standard hyphen positions. To require the 36 character form with the version and variant checks above, use the regex with fullmatch.
Validate at the edge, trust inside
Check UUIDs once, where untrusted input enters: request parameters, form fields, file imports. After that, pass them around as a typed UUID value rather than a string so you never need to check again. Databases with a native uuid column will also reject malformed values for you.
Need sample values to test your validator? Grab a few from our UUID generator, then try breaking them by deleting a character. Or skip the regex and paste them into our validator and decoder, which reports the version and variant of each value.