RRegexBuilder
Get RegexBuilder

RegexBuilder · regex answers

Regex for a UUID

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

A canonical UUID is 8-4-4-4-12 hex digits. The third group starts with the version digit and the fourth with the variant nibble, which is 8, 9, a or b for the variant every common library emits. Versions 1 through 8 are defined today.

What each part does

[0-9a-f]{8}the first group, eight hex digits
[1-8]the version digit
[89ab]the variant nibble
icase-insensitive, since UUIDs are printed in both cases

Tested against real input

Every row below was run through the engine when this page was built. The third column is the substring the pattern actually matched.

InputResultMatched
550e8400-e29b-41d4-a716-446655440000matches550e8400-e29b-41d4-a716-446655440000
01890a5d-ac96-774b-bcce-b302099a8057matches01890a5d-ac96-774b-bcce-b302099a8057
550e8400-e29b-41d4-a716-44665544000no match
550e8400e29b41d4a716446655440000no match
550e8400-e29b-01d4-a716-446655440000no match

The mistake to avoid

Accepting the nil UUID 00000000-0000-0000-0000-000000000000 by using [0-9a-f]{4} for every group — it is a valid identifier but almost never a valid value in your system.

Try it on your own text

Paste this pattern and your input, see each match highlighted with its capture groups, step through what the engine did, and get the equivalent syntax for JavaScript, Python, Java, Go and PCRE.

Open RegexBuilder

Other regex answers