UUID regex

Regex to validate UUIDs/GUIDs in 8-4-4-4-12 format, with a strict version that checks the version and variant digits.

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

Matches

  • 123e4567-e89b-42d3-a456-426614174000
  • 01890a5d-ac96-774b-bcce-b302099a8057
  • F47AC10B-58CC-4372-A567-0E02B2C3D479

Doesn't match

  • 123e4567e89b42d3a456426614174000
  • 123e4567-e89b-02d3-a456-426614174000
  • g47ac10b-58cc-4372-a567-0e02b2c3d479

Alternative: Any 8-4-4-4-12 hex (loose)

^[0-9a-f]{8}-(?:[0-9a-f]{4}-){3}[0-9a-f]{12}$

Notes

  • The 13th hex digit is the version (1–8 per RFC 9562); the 17th is the variant (8, 9, a or b).
  • Need some? The UUID generator makes v4 and v7 in bulk.

Code

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;
re.test(value); // true or false

Python

import re

bool(re.fullmatch(r'[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}', value, re.I))

Try it on your own input

The live tester adds the g and m flags so every line is checked separately.

Questions

Is the nil UUID valid?

00000000-0000-0000-0000-000000000000 is defined by the RFC but fails the strict pattern (version 0). Use the loose pattern if you must accept it.

More patterns

Email regexPhone number regexURL regexIP address regexDate regexTime regex (24-hour and 12-hour)Hex color regexPassword regex