RegexCraft

UUID regex

Matches the 8-4-4-4-12 hex form of a UUID. The version digit must be 1 to 8 and the variant digit 8, 9, a or b, as RFC 9562 defines, so random hex strings of the right shape are rejected. The i flag accepts upper and lower case.

Open in the tester

The pattern

/\b[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}\b/gi

Sample text, 2 matches

Request 3f2b6c1e-9a4d-4f7b-8c2e-1d5a7b9e0f42 was retried as 018F6D2A-7C3B-7E41-9B2D-5A6C8E0F1A23, but 3f2b6c1e-9a4d-4f7b-cc2e-1d5a7b9e0f42 has the wrong variant.

What it deliberately doesn’t do

  • Braced ({...}) and URN (urn:uuid:...) forms are not matched.
  • The nil and max UUIDs (all 0s, all fs) fail the version check.

Explained, in order

  1. \bWord boundary — asserts a boundary between a word character and a non-word character. Consumes no characters.
  2. [0-9a-f]Character class — matches one character from: '0' through '9', 'a' through 'f'
  3. {8}The preceding element matches exactly 8 times (greedy)
  4. -Matches the character '-' literally
  5. [0-9a-f]Character class — matches one character from: '0' through '9', 'a' through 'f'
  6. {4}The preceding element matches exactly 4 times (greedy)
  7. -Matches the character '-' literally
  8. [1-8]Character class — matches one character from: '1' through '8'
  9. [0-9a-f]Character class — matches one character from: '0' through '9', 'a' through 'f'
  10. {3}The preceding element matches exactly 3 times (greedy)
  11. -Matches the character '-' literally
  12. [89ab]Character class — matches one character from: '8', '9', 'a', 'b'
  13. [0-9a-f]Character class — matches one character from: '0' through '9', 'a' through 'f'
  14. {3}The preceding element matches exactly 3 times (greedy)
  15. -Matches the character '-' literally
  16. [0-9a-f]Character class — matches one character from: '0' through '9', 'a' through 'f'
  17. {12}The preceding element matches exactly 12 times (greedy)
  18. \bWord boundary — asserts a boundary between a word character and a non-word character. Consumes no characters.

In your language

JavaScript

The engine this page was tested with.

const re = /\b[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}\b/gi;

// Example
const matches = [...str.matchAll(/\b[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}\b/gi)];
for (const m of matches) {
  console.log(m[0], m.index, m.groups);
}

Python

Translated. Named groups are rewritten to (?P<name>…).

import re

pattern = re.compile(r"\b[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}\b", re.IGNORECASE)

# Example
for m in pattern.finditer(text):
    print(m.group(0), m.start(), m.groups())

Go

Translated. Go's RE2 has no lookaround or backreferences.

package main

import (
	"fmt"
	"regexp"
)

// Note: Inline flags at the start of the pattern: (?i), (?m), (?s).

var re = regexp.MustCompile(`(?i)\b[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}\b`)

func main() {
	for _, match := range re.FindAllStringSubmatch(text, -1) {
		fmt.Println(match[0], match)
	}
}

Rust

Translated. The regex crate has no lookaround or backreferences.

use regex::Regex;

// Note: Rust finds all matches via find_iter / captures_iter (no 'g' flag).

fn main() {
    let re = Regex::new(r#"(?i)\b[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}\b"#).unwrap();

    for m in re.find_iter(text) {
        println!("{} at {}", m.as_str(), m.start());
    }
}