Home/UUID Regex
Tested patterns · RFC 9562 · Live tester

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.

Quick answer: this regex matches any standard UUID. Use it with the case-insensitive flag.^[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:

Regex
^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$
  • ^ 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 caseRegex (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.

The GUID pattern uses two branches on purpose. A shortcut like ^\{?…\}?$ would also accept unbalanced values such as {f47ac10b-… with no closing brace.

UUID regex in JavaScript and TypeScript

JavaScript
const UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i; UUID_RE.test(‘f47ac10b-58cc-4372-a567-0e02b2c3d479’); // true // Find every UUID in a string const all = text.match(/\b[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\b/gi) || [];

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
import re UUID_RE = re.compile(r’^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$’, re.IGNORECASE) bool(UUID_RE.fullmatch(‘f47ac10b-58cc-4372-a567-0e02b2c3d479’)) # True

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
$re = ‘[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}’ # Test one value (-match is case-insensitive) ‘F47AC10B-58CC-4372-A567-0E02B2C3D479’ -match “^$re$” # True # Extract every UUID from a file Select-String -Path .\log.txt -Pattern $re -AllMatches | ForEach-Object { $_.Matches.Value }

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

Java
Pattern UUID_RE = Pattern.compile( “^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$”, Pattern.CASE_INSENSITIVE); boolean ok = UUID_RE.matcher(value).matches();
C#
bool ok = Regex.IsMatch(value, @”^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$”, RegexOptions.IgnoreCase); // Or, without a regex, for the 36-character format only: bool ok2 = Guid.TryParseExact(value, “D”, out _);
Go
var uuidRE = regexp.MustCompile(`(?i)^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$`) ok := uuidRE.MatchString(value)
PHP
$ok = preg_match(‘/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i’, $value) === 1;
Ruby
UUID_RE = /\A[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\z/i UUID_RE.match?(value)

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

PostgreSQL
SELECT * FROM events WHERE ref ~* ‘^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$’;
MySQL 8.0+
SELECT * FROM events WHERE REGEXP_LIKE(ref, ‘^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$’, ‘i’);
Snowflake
— Extract the first UUID from a text column SELECT REGEXP_SUBSTR(message, ‘[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}’, 1, 1, ‘i’) AS uuid FROM logs;
grep
# Print every UUID found in a file grep -oiE ‘[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}’ app.log

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

HTML
<input name=”id” required pattern=”[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}”>

The pattern attribute always matches the whole value and has no flags, so include both a-f and A-F.

JSON Schema
{ “type”: “string”, “format”: “uuid” }

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.