Start free · Free tools · Fair usage limits
Browse Tools

Categories

Custom AI Solutions – AI2Flows
LIVE NEWS
Loading latest AI & SEO news…
Home›Tools›Regex Tester›Regex for semantic version (SemVer)
Regex pattern · validation

Regex for semantic version (SemVer)

Regex for semantic versioning: a tested pattern with examples, a plain-English breakdown, and code for JavaScript, Python, PHP, Java and Go.

Quick answer

Use the pattern below as a semantic version (SemVer) regex. It matches "1.0.0" and "2.1.3-beta.1", and rejects "1.0" and "01.0.0". This is the official pattern published at semver.org.

Last updated · Maintained by FreeAIToolz · How we test

/^(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-]+)*))?$/
  • 1.0.0matches
  • 2.1.3-beta.1matches
  • 1.0.0+build.5matches
  • 1.0.0-rc.1+sha.abc123matches
  • 1.0no match
  • 01.0.0no match
  • 1.0.0-no match
  • v1.0.0no match
  • 1.0.0-01no match
Open in the full Regex Tester (replace, flags, groups) →

How this regex works

PartMeaning
(0|[1-9]\d*)MAJOR, MINOR and PATCH numbers without leading zeros
(?:-(...))?optional pre-release such as -beta.1
(?:\+(...))?$optional build metadata such as +build.5

Examples

InputResult
1.0.0✓ Matches
2.1.3-beta.1✓ Matches
1.0.0+build.5✓ Matches
1.0.0-rc.1+sha.abc123✓ Matches
1.0✗ No match
01.0.0✗ No match
1.0.0-✗ No match
v1.0.0✗ No match
1.0.0-01✗ No match

Every example above is checked automatically in JavaScript and Python each time the site is built.

Limitations and edge cases

This is the official pattern published at semver.org. Tags such as v1.0.0 fail; strip the v first.

When to use this pattern

Release pipelines, package tooling and version fields in APIs.

Use it in your language

JavaScript

const re = /^(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-]+)*))?$/;
re.test(input); // true or false

Python

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.search(value))

PHP

$ok = preg_match('~^(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-]+)*))?$~', $value) === 1;

Java

Pattern pattern = Pattern.compile("^(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-]+)*))?$");
boolean ok = pattern.matcher(value).find();

Go

re := 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-]+)*))?$`)
ok := re.MatchString(value)

More regex patterns

Frequently asked questions

What is the regex for semantic version (SemVer)?
See the pattern at the top of this page. This is the official pattern published at semver.org. Tags such as v1.0.0 fail; strip the v first.
Which languages does this pattern work in?
It is tested in JavaScript and Python and uses syntax that also works in PHP (PCRE) and Java. It also works in Go’s RE2 engine.
Why does the pattern start with ^ and end with $?
They anchor the match to the whole string, so "abc1.0.0xyz" cannot pass just because it contains a valid part. To search for matches inside longer text, remove ^ and $ (use \b where word boundaries help) and add the g flag.