Semantic Version (SemVer)Regex — Pattern, Examples & Code
^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$What it matches / doesn't match
| Matches ✅ | Does not match ❌ |
|---|---|
●1.0.0 | ●1.0 |
●2.3.1-alpha.1 | ●01.2.3 |
●0.1.0+build.456 | ●1.0.0- |
How it works
Implements the official SemVer 2.0.0 specification. The three numeric parts disallow leading zeros (except the literal 0). Optional pre-release identifiers (after -) and build metadata (after +) follow the SemVer rules for alphanumeric and hyphen characters. This is the canonical SemVer regex from semver.org.
Code Usage
JavaScript
const regex = /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$/;
regex.test("1.0.0"); // truePython
import re
pattern = re.compile(r'^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$')
bool(pattern.match("1.0.0")) # TrueGo
import "regexp"
r := regexp.MustCompile(`^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$`)
r.MatchString("1.0.0") // trueNeed a custom regex for a different use case?
Generate a custom regex with AI →FAQ
What does the Semantic Version (SemVer) regex pattern match?
Implements the official SemVer 2.0.0 specification. The three numeric parts disallow leading zeros (except the literal 0). Optional pre-release identifiers (after -) and build metadata (after +) follow the SemVer rules for alphanumeric and hyphen characters. This is the canonical SemVer regex from semver.org.
How do I use the Semantic Version (SemVer) regex in JavaScript?
const regex = /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$/; regex.test("1.0.0"); // true
Can I use the Semantic Version (SemVer) regex in Python?
Yes. import re pattern = re.compile(r'^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$') bool(pattern.match("1.0.0")) # True
