tools / regex library / semver version
regex pattern · ready to copy

Regex for matching Semantic Versioning (semver) versions

Match semver-compliant versions: MAJOR.MINOR.PATCH (+ pre-release + build).

intermediate javascript / pcre / python4 use cases

The pattern

^(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-]+)*))?$

Test cases

InputResult
1.0.0matches
2.5.3-beta.1matches
1.0.0-rc.1+build.123matches
0.0.1matches
1.0 (missing patch)rejects
v1.0.0 (leading v — strip first)rejects
01.0.0 (leading zero)rejects

Edge cases & caveats

Strip leading `v` before matching (npm convention adds it). For npm ranges (`^1.2.3`, `~2.0`), use the official `semver` package — regex can't compare ranges.

Common use cases

Try variations against your data
regexlab is a free in-browser tester with side-by-side match highlighting, group inspector, and named-capture export to JS/Python/PCRE.
Open regexlab  

Related

ipv6 address · strong password · jwt · url