Regex for Semantic Version (SemVer) — Pattern Explained with Examples
Regex for Semantic Version (SemVer) — Pattern Explained with Examples
DodaTech
Updated Jun 20, 2026
2 min read
Semantic Versioning (SemVer) is a versioning scheme used by most modern software packages. This pattern validates the MAJOR.MINOR.PATCH format with optional pre-release labels and build metadata according to the SemVer 2.0.0 specification.
The Pattern
/^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(-[a-zA-Z0-9.-]+)?(\+[a-zA-Z0-9.-]+)?$/Pattern Breakdown
| Part | Meaning |
|---|---|
^ | Start-of-string anchor |
(0|[1-9]\d*) | MAJOR: zero or a positive integer without leading zeros |
\. | Literal dot separator |
(0|[1-9]\d*) | MINOR: zero or a positive integer without leading zeros |
\. | Literal dot separator |
(0|[1-9]\d*) | PATCH: zero or a positive integer without leading zeros |
(-[a-zA-Z0-9.-]+)? | Optional pre-release: hyphen followed by alphanumeric, dots, hyphens |
(\+[a-zA-Z0-9.-]+)? | Optional build metadata: plus sign followed by alphanumeric, dots, hyphens |
$ | End-of-string anchor |
Matches
1.0.02.1.30.0.11.0.0-alpha2.0.0+build.20241.2.3-beta.1+build99
Does NOT Match
1.01.0.0.0v1.0.001.0.01.0.0-alpha.(trailing dot)1.0.0-alpha..beta
Language Examples
JavaScript
const semverRegex = /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(-[a-zA-Z0-9.-]+)?(\+[a-zA-Z0-9.-]+)?$/;
console.log(semverRegex.test('1.0.0')); // true
console.log(semverRegex.test('01.0.0')); // false
console.log(semverRegex.test('1.0.0-alpha.1')); // true
Python
import re
pattern = r'^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(-[a-zA-Z0-9.-]+)?(\+[a-zA-Z0-9.-]+)?$'
print(bool(re.match(pattern, '1.0.0'))) # True
print(bool(re.match(pattern, '01.0.0'))) # False
print(bool(re.match(pattern, '1.0.0-beta+exp'))) # TrueCommon Pitfalls
- Leading zeros are prohibited in SemVer —
01.0.0is invalid because01is parsed as1but the spec forbids leading zeros for unambiguous sorting - Pre-release precedence is lower than the release version (
1.0.0-alpha<1.0.0), but build metadata is ignored for precedence — this pattern validates syntax only - The
vprefix (e.g.,v1.0.0) is common in git tags but is not part of the SemVer spec — strip the prefix before validation or addv?at the start - Pre-release and build metadata identifiers are dot-separated; consecutive dots (
1.0.0-alpha..1) are invalid - Each pre-release or build identifier must be alphanumeric or hyphen — identifiers with spaces or special characters are rejected
Real-World Use Cases
- npm package validation — ensure
package.jsonversion fields conform to SemVer before publishing - CI/CD pipeline checks — validate version tags in git before building and deploying releases
- API version negotiation — parse and compare API version strings in HTTP headers for routing requests
FAQ
Related Patterns
Regex for Numbers Regex for Date (YYYY-MM-DD)
Previous
Regex for Canadian Postal Code — Pattern Explained with Examples
Next
Regex for ISBN — Pattern Explained with Examples
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro