Regex for Semantic Version

All patterns
Validation121 chars

Regex for Semantic Version

Semantic version matching is useful in release tooling, changelog parsing, and dependency dashboards where you need to distinguish versions from free text.

The expression

/^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-[0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*)?(?:\+[0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*)?$/

How to read it

Anchor: the match must start at the beginning of the string.

Anchor: the match must run to the end of the string.

\d matches a single digit, 0 through 9.

(?: … ) groups without capturing, so it costs nothing to read back.

Nested quantifiers — the shape behind catastrophic backtracking.

Validate your own text, line by line

no input
Try:

This expression is anchored, so it asks whether a whole string is valid. Each line above is tested on its own — it will not find a match inside a longer sentence.

Examples, checked in your browser

4/4 verified

1.2.3

Matches.

2.0.0-beta.1+build7

Matches.

01.2.3

No match — this is the kind of input the pattern rejects.

v1.2

No match — this is the kind of input the pattern rejects.

Use it in code

3 languages
JavaScript
const regex = /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-[0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*)?(?:\+[0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*)?$/g;
regex.test(input);
Python
import re
pattern = re.compile(r"^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-[0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*)?(?:\+[0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*)?$")
bool(pattern.search(input))
Go
re := regexp.MustCompile("^(0|[1-9]\\d*)\\.(0|[1-9]\\d*)\\.(0|[1-9]\\d*)(?:-[0-9A-Za-z-]+(?:\\.[0-9A-Za-z-]+)*)?(?:\\+[0-9A-Za-z-]+(?:\\.[0-9A-Za-z-]+)*)?$")
matched := re.MatchString(input)

Other validation patterns

6
GroupValidation
Flagsnone
Examples4
SafetyOK