RegexCraft

Semantic version (semver) regex

Matches semantic versions with an optional leading v. Major, minor and patch are captured without leading zeros, then an optional pre-release after a hyphen and optional build metadata after a plus.

Open in the tester

The pattern

/\bv?(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-([\w.-]+))?(?:\+([\w.-]+))?\b/g

Sample text, 4 matches

1.0.0 and 2.14.3 are stable, 1.0.0-beta.1 is a pre-release, and 3.0.0+build.20240501 carries build metadata.

Point at a coloured group to see it in the pattern, the text and the explanation.

What it deliberately doesn’t do

  • The pre-release and build parts are looser than the semver.org grammar allows.
  • Ranges and operators (^1.2, ~1.2.3, >=2) are not versions and are not matched.

Explained, in order

  1. \bWord boundary — asserts a boundary between a word character and a non-word character. Consumes no characters.
  2. vMatches the character 'v' literally
  3. ?The preceding element matches optionally (zero or one time) (greedy)
  4. (0|[1-9]\d*)Group 1.Capturing group #1 — groups the pattern and captures the matched text for later reference
  5. 0|[1-9]\d*Alternation — matches any ONE of the 2 alternatives listed above/below
  6. 0Matches the character '0' literally
  7. [1-9]Character class — matches one character from: '1' through '9'
  8. \dMatches a digit (0–9)
  9. *The preceding element matches zero or more times (greedy)
  10. \.Matches the character '.' literally
  11. (0|[1-9]\d*)Group 2.Capturing group #2 — groups the pattern and captures the matched text for later reference
  12. 0|[1-9]\d*Alternation — matches any ONE of the 2 alternatives listed above/below
  13. 0Matches the character '0' literally
  14. [1-9]Character class — matches one character from: '1' through '9'
  15. \dMatches a digit (0–9)
  16. *The preceding element matches zero or more times (greedy)
  17. \.Matches the character '.' literally
  18. (0|[1-9]\d*)Group 3.Capturing group #3 — groups the pattern and captures the matched text for later reference
  19. 0|[1-9]\d*Alternation — matches any ONE of the 2 alternatives listed above/below
  20. 0Matches the character '0' literally
  21. [1-9]Character class — matches one character from: '1' through '9'
  22. \dMatches a digit (0–9)
  23. *The preceding element matches zero or more times (greedy)
  24. (?:-([\w.-]+))Non-capturing group — groups the pattern without capturing
  25. -Matches the character '-' literally
  26. ([\w.-]+)Group 4.Capturing group #4 — groups the pattern and captures the matched text for later reference
  27. [\w.-]Character class — matches one character from: a word character (letter, digit, or underscore), '.', '-'
  28. +The preceding element matches one or more times (greedy)
  29. ?The preceding element matches optionally (zero or one time) (greedy)
  30. (?:\+([\w.-]+))Non-capturing group — groups the pattern without capturing
  31. \+Matches the character '+' literally
  32. ([\w.-]+)Group 5.Capturing group #5 — groups the pattern and captures the matched text for later reference
  33. [\w.-]Character class — matches one character from: a word character (letter, digit, or underscore), '.', '-'
  34. +The preceding element matches one or more times (greedy)
  35. ?The preceding element matches optionally (zero or one time) (greedy)
  36. \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 = /\bv?(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-([\w.-]+))?(?:\+([\w.-]+))?\b/g;

// Example
const matches = [...str.matchAll(/\bv?(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-([\w.-]+))?(?:\+([\w.-]+))?\b/g)];
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"\bv?(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-([\w.-]+))?(?:\+([\w.-]+))?\b")

# 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"
)

var re = regexp.MustCompile(`\bv?(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-([\w.-]+))?(?:\+([\w.-]+))?\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#"\bv?(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-([\w.-]+))?(?:\+([\w.-]+))?\b"#).unwrap();

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