Code
Semantic version (SemVer) Regex
Semantic Versioning writes releases as MAJOR.MINOR.PATCH, optionally followed by a pre-release label after a dash and build metadata after a plus sign. This pattern follows the SemVer 2.0.0 grammar closely: numbers have no leading zeros, numeric pre-release parts have no leading zeros either, and identifiers use only letters, digits and hyphens. A leading v is allowed because git tags often include it. Capture groups return the major, minor, patch, pre-release and build parts. It is useful for release scripts, changelog tools and package manifests. It checks format only and cannot compare or order two versions.
Pattern (JavaScript)
/^v?(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-]+)*))?$/Starts at the start of the line/string, then the character "v", optionally (zero or one time), then a group (the character "0" OR one of the characters 1-9, then a digit (0-9), zero or more times), then the character ".", then a group (the character "0" OR one of the characters 1-9, then a digit (0-9), zero or more times), then the character ".", then a group (the character "0" OR one of the characters 1-9, then a digit (0-9), zero or more times), then (the character "-", then a group ((the character "0" OR one of the characters 1-9, then a digit (0-9), zero or more times OR a digit (0-9), zero or more times, then one of the characters a-zA-Z-, then one of the characters 0-9a-zA-Z-, zero or more times), then (the character ".", then (the character "0" OR one of the characters 1-9, then a digit (0-9), zero or more times OR a digit (0-9), zero or more times, then one of the characters a-zA-Z-, then one of the characters 0-9a-zA-Z-, zero or more times)), zero or more times)), optionally (zero or one time), then (the character "+", then a group (one of the characters 0-9a-zA-Z-, one or more times, then (the character ".", then one of the characters 0-9a-zA-Z-, one or more times), zero or more times)), optionally (zero or one time), then the end of the line/string.
Examples
Matches
- ✓ 1.0.0
- ✓ v2.10.3-beta.1
- ✓ 1.0.0-alpha+build.5
- ✓ 0.0.1-rc.1+sha.a1b2c3d
Does not match
- ✗ 1.0
- ✗ 01.2.3
- ✗ 1.2.3-01
- ✗ 1.2.3-beta..1
Try it
0 matches (JavaScript engine)
1.0.0
v2.10.3-beta.1
1.0.0-alpha+build.5
0.0.1-rc.1+sha.a1b2c3d
1.0
01.2.3
1.2.3-01
1.2.3-beta..1Code in 8 languages
Converted automatically from the JavaScript pattern; language-specific notes are shown where the syntax or support differs.
JavaScript
const re = /^v?(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-]+)*))?$/g;
const text = "1.0.0";
const matches = [...text.matchAll(re)].map((m) => m[0]);
console.log(matches);Python
import re
pattern = re.compile(r'^v?(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-]+)*))?$')
text = '1.0.0'
matches = [m.group(0) for m in pattern.finditer(text)]
print(matches)PHP
<?php
$pattern = '/^v?(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-]+)*))?$/';
$text = '1.0.0';
preg_match_all($pattern, $text, $matches);
print_r($matches[0]);Java
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
Pattern pattern = Pattern.compile("^v?(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-]+)*))?$");
String text = "1.0.0";
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}Go
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`^v?(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-]+)*))?$`)
text := "1.0.0"
fmt.Println(re.FindAllString(text, -1))
}C#
using System;
using System.Text.RegularExpressions;
var pattern = new Regex(@"^v?(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-]+)*))?$");
var text = "1.0.0";
foreach (Match m in pattern.Matches(text))
{
Console.WriteLine(m.Value);
}Rust
// Cargo.toml: regex = "1"
use regex::Regex;
fn main() {
let re = Regex::new(r"^v?(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-]+)*))?$").unwrap();
let text = "1.0.0";
for m in re.find_iter(text) {
println!("{}", m.as_str());
}
}- Add regex = "1" under [dependencies] in Cargo.toml.
Ruby
pattern = /^v?(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-]+)*))?$/
text = '1.0.0'
matches = text.to_enum(:scan, pattern).map { Regexp.last_match[0] }
p matchesFrequently asked questions
- Why is 01.2.3 rejected?
- SemVer forbids leading zeros in the major, minor and patch numbers, because 01 and 1 would otherwise be ambiguous. The pattern uses (0|[1-9]\d*) to enforce that rule.
- Is the leading v part of the version?
- Strictly no; the specification says v1.2.3 is a tag name, not a version. The pattern accepts it because git tags and release pages commonly include it.
- Can I sort versions with this regex?
- No. The regex splits a version into parts, but ordering needs numeric comparison and special rules for pre-release labels. Use a semver library for sorting and range checks.