PatternRegex

Match a UUID v4

Match version-4 UUIDs precisely: this pattern checks the version digit and variant bits, not just the 8-4-4-4-12 dash layout.

Last updated

Patterng
[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}
Open in Regex Tester & Builder

Matches

  • 9b2d8e1a-3f4c-4d5e-9f6a-7b8c9d0e1f2a
  • AB12CD34-EF56-4A7B-8C9D-0E1F2A3B4C5D

Doesn’t match

  • 12345678-1234-1234-1234-123456789012
  • 9b2d8e1a3f4c4d5e9f6a7b8c9d0e1f2a

Why it’s written this way

The third block must start with 4 — that IS the version field. The fourth block must start with 8, 9, a or b, which is the RFC 4122 variant. Checking both is what distinguishes "a real v4 UUID" from "32 hex digits wearing dashes".

Everything else is plain hex in the standard 8-4-4-4-12 grouping.

Edge cases to know

  • Other UUID versions (v1 timestamps, v5 hashes, v7) deliberately fail — drop the two literal checks if you want any-version matching.
  • Case-insensitive by character class rather than the i flag, so it behaves the same wherever you paste it.
  • The all-zero nil UUID does not match — its version digit is 0.

Related in Patterns