Regex patterns/Validation

UUID (version 1–5)

The canonical 8-4-4-4-12 form, with the version and variant digits actually checked.

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

The problem

Check that a string is a canonically formatted UUID before using it as a key.

How it reads

start[0-9a-f]repeat-[0-9a-f]repeat-[1-5][0-9a-f]repeat-[89ab][0-9a-f]repeat-[0-9a-f]repeatend

Follow the line from left to right — every path you can trace is a string this pattern matches.

  1. ^[0-9a-f]{8}-[0-9a-f]…In order:
  2. ^The start of the text (or of a line with the m flag)
  3. [0-9a-f]{8}Any one of: "0" to "9" or "a" to "f", exactly 8 times, as many as possible
  4. -The character "-"
  5. [0-9a-f]{4}Any one of: "0" to "9" or "a" to "f", exactly 4 times, as many as possible
  6. -The character "-"
  7. [1-5]Any one of: "1" to "5"
  8. [0-9a-f]{3}Any one of: "0" to "9" or "a" to "f", exactly 3 times, as many as possible
  9. -The character "-"
  10. [89ab]Any one of: "8", "9", "a" or "b"
  11. [0-9a-f]{3}Any one of: "0" to "9" or "a" to "f", exactly 3 times, as many as possible
  12. -The character "-"
  13. [0-9a-f]{12}Any one of: "0" to "9" or "a" to "f", exactly 12 times, as many as possible
  14. $The end of the text (or of a line with the m flag)

Matches

  • 123e4567-e89b-12d3-a456-426614174000
  • 123E4567-E89B-42D3-A456-426614174000

Does not match

  • not-a-uuid
  • 123e4567e89b12d3a456426614174000
  • 123e4567-e89b-02d3-a456-426614174000

Where it bites

  • The third group starts with [1-5] (the version) and the fourth with [89ab] (the variant). A nil UUID of all zeros is therefore rejected — add 0 to both sets if you need it.
  • Without the i flag, uppercase UUIDs fail. They are valid.
  • Matching the shape says nothing about whether the UUID exists in your database.

More validation patterns