Regex to Match a UUID v4
Copy the pattern, paste your text below, and find only the version-4 UUIDs. Below the tester: which two nibbles mark a v4 UUID and how this differs from an any-version pattern.
What this pattern matches
This expression matches only version-4 UUIDs — the random ones most applications generate. It differs from an any-version UUID pattern by pinning the two nibbles that encode the version and variant.
How it works, part by part
[0-9a-fA-F]{8}-[0-9a-fA-F]{4} — the first two groups, any hex.
4[0-9a-fA-F]{3} — the third group must start with 4, the version marker.
[89abAB][0-9a-fA-F]{3} — the fourth group must start with 8, 9, a, or b, the RFC 4122 variant.
[0-9a-fA-F]{12} — the final node.
v4 versus any version
A generic UUID pattern accepts any hex in the version and variant positions, so it also matches v1 (time-based) or v5 (name-based) values. Pin those nibbles as above when you specifically need randomly generated identifiers and want to reject the others.
Use it in your code
import re
text = "v4: 9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d, v1: 6ba7b810-9dad-11d1-80b4-00c04fd430c8 (skipped)."
pattern = r'''[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}'''
for m in re.finditer(pattern, text):
print(m.group())
const text = "v4: 9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d, v1: 6ba7b810-9dad-11d1-80b4-00c04fd430c8 (skipped).";
const re = new RegExp(String.raw`[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}`, "g");
console.log(text.match(re));
FAQ
Why must the variant nibble be 8, 9, a, or b?
RFC 4122 sets the two high bits of that nibble to 10, which in hex is exactly 8 through b.
Is it case-insensitive?
Yes. Every group accepts both a-f and A-F, and the variant class includes A and B, so mixed-case UUIDs match.
How do I match any UUID version instead?
Replace 4 with [1-8] in the version group and [89abAB] with [0-9a-fA-F] in the variant group.