UUID Regex
Copy a tested regular expression for UUIDs and GUIDs — any version, strict RFC 9562, v4 or v7 — with examples for 14 languages and tools, and test it live in your browser.
^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$UUID regex tester
The standard UUID regex
A UUID is 32 hexadecimal digits in five groups of 8-4-4-4-12, separated by hyphens. This pattern matches exactly that:
- ^ and $ anchor the match, so extra characters before or after fail.
- [0-9a-f] allows only hex digits. Turn on the case-insensitive flag, because RFC 9562 says uppercase input must be accepted.
- If your tool has no case-insensitive flag, use [0-9a-fA-F] instead.
Regex patterns for every case
| Use case | Regex (case-insensitive) |
|---|---|
| Any UUID | ^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$ |
| Strict RFC 9562 | ^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$ |
| UUID v4 only | ^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$ |
| UUID v7 only | ^[0-9a-f]{8}-[0-9a-f]{4}-7[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$ |
| No hyphens | ^[0-9a-f]{32}$ |
| GUID with optional braces | ^(?:\{[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\}|[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})$ |
| Find UUIDs inside text | \b[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\b (global flag) |
The strict pattern checks two things the simple one does not: the version digit (the first digit of the third group, 1 to 8) and the variant (the first digit of the fourth group: 8, 9, a or b). Note that it rejects the Nil and Max UUIDs, which have no version. Add them separately if you need to accept them.
UUID regex in JavaScript and TypeScript
With the Valibot library, use its built-in check: v.pipe(v.string(), v.uuid()). See the JavaScript UUID guide for generating UUIDs.
UUID regex in Python
Python’s uuid.UUID(value) also validates, but it accepts several formats (no hyphens, braces, urn:uuid:). Use the regex when you need exactly the 36-character form.
In Django, you do not need a regex in URLs. The built-in path converter matches lowercase UUIDs: path(‘items/<uuid:pk>/’, view).
UUID regex in PowerShell
PowerShell’s -match and Select-String are case-insensitive by default. For a case-sensitive match, use -cmatch or add -CaseSensitive. You can also validate with [guid]::TryParse($value, [ref]$null).
UUID regex in Java, C#, Go, PHP and Ruby
Ruby tip: use \A and \z, not ^ and $. In Ruby, ^ and $ match at every line break, so a value with an extra line could still pass.
In Java, UUID.fromString() is lenient about group lengths in some versions, so use the regex when you need the strict format. More examples are in the Java, Go and PHP guides.
UUID regex in SQL, Snowflake and grep
If you store UUIDs in a native column type (PostgreSQL uuid or SQL Server uniqueidentifier), the database already rejects invalid values, so you only need a regex for text columns. See the PostgreSQL and MySQL guides.
HTML forms and JSON Schema
The pattern attribute always matches the whole value and has no flags, so include both a-f and A-F.
Common UUID regex mistakes
- No anchors. Without ^ and $, the value xx-f47ac10b-…-0e02b2c3d479-xx would pass.
- Forgetting uppercase. GUIDs from Windows and .NET are often uppercase. Use the case-insensitive flag or [0-9a-fA-F].
- Using \w or [a-z]. These allow letters like g and z, which are never valid in a UUID.
- Expecting a version check from the simple pattern. Only the strict pattern checks the version and variant digits.
- Using a regex when a parser exists. Native types and functions like uuid.UUID() or Guid.TryParse() are often clearer, but they accept more formats than the strict 36-character form.
For a quick check of a single value without writing any code, use the UUID validator or the UUID decoder.
Frequently asked questions
^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$ with the case-insensitive flag. It matches any standard 36-character UUID.
^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$ with the case-insensitive flag. It requires 4 as the version digit and 8, 9, a or b as the variant digit.
It should not be. RFC 9562 says UUIDs should be output in lowercase but uppercase must be accepted on input, so use a case-insensitive flag or include A-F in the character class.
Use two branches so the braces must be balanced: one for the braced form and one for the plain form, as shown in the pattern table on this page.
The simple pattern does, because the Nil UUID is all hex zeros. The strict RFC 9562 pattern does not, because the Nil UUID has no version digit.