Regex to Match a UUID
Copy the pattern, paste your text below, and see every UUID it finds. Below the tester: the 8-4-4-4-12 layout and how to make it case- or version-strict.
Regex pattern
/\b[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}\b/g
This matches a canonical UUID (8-4-4-4-12 hex digits, any version), e.g. 550e8400-e29b-41d4-a716-446655440000; add the (i) flag for either case. For UUID v4 specifically, use ^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-4[0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$ — it pins the version digit 4 and the variant nibble [89ab]. Both check format only, not UUID validity.
What this pattern matches
This matches a canonical UUID: five hex groups of 8, 4, 4, 4, and 12 characters separated by hyphens. It accepts every version (v1–v8) and both letter cases.
How it works, part by part
[0-9a-fA-F]{8} — the first group of eight hex digits.
-[0-9a-fA-F]{4} — three middle groups of four hex digits each.
-[0-9a-fA-F]{12} — the final group of twelve hex digits.
Version-strict matching
To require a specific version, pin the first digit of the third group and the UUID variant. A full UUID v4 pattern is ^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-4[0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$: the 4 fixes the version and [89abAB] fixes the variant nibble (RFC 4122). Drop the [89abAB] constraint to accept any variant.
Use it in your code
import re
text = "Valid: 123e4567-e89b-12d3-a456-426614174000. Invalid: 123e4567-e89b-12d3-a456 (missing group)."
pattern = r'''\b[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}\b'''
for m in re.finditer(pattern, text):
print(m.group())
const text = "Valid: 123e4567-e89b-12d3-a456-426614174000. Invalid: 123e4567-e89b-12d3-a456 (missing group).";
const re = new RegExp(String.raw`\b[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}\b`, "g");
console.log(text.match(re));
FAQ
Does it match all UUID versions?
Yes. It accepts any hex in the version and variant positions, covering v1 through v8.
How do I match only UUID v4?
Replace the third group with 4[0-9a-fA-F]{3} to require the version-4 marker.
Is it case-insensitive?
Yes. The class includes both a-f and A-F, so upper and lowercase UUIDs both match.