Skip to content
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

PartMeaning
^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.0
  • 2.1.3
  • 0.0.1
  • 1.0.0-alpha
  • 2.0.0+build.2024
  • 1.2.3-beta.1+build99

Does NOT Match

  • 1.0
  • 1.0.0.0
  • v1.0.0
  • 01.0.0
  • 1.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')))  # True

Common Pitfalls

  • Leading zeros are prohibited in SemVer — 01.0.0 is invalid because 01 is parsed as 1 but 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 v prefix (e.g., v1.0.0) is common in git tags but is not part of the SemVer spec — strip the prefix before validation or add v? 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.json version 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

What is the difference between pre-release and build metadata?
Pre-release identifiers (after -) denote instability — 1.0.0-alpha has lower precedence than 1.0.0. Build metadata (after +) contains build-specific information like timestamps or commit hashes and is ignored when comparing versions. Build metadata does not affect precedence.
Why are leading zeros not allowed in SemVer?
Leading zeros are disallowed to prevent ambiguity in version sorting. Without this rule, 01.0.0 could be interpreted as equivalent to 1.0.0 or as a separate version. The rule ensures each version component is a canonical integer without formatting variations.

Related Patterns

Regex for Numbers Regex for Date (YYYY-MM-DD)

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro