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
| Input | Result |
|---|---|
| 1.0.0 | ✓matches |
| 2.5.3-beta.1 | ✓matches |
| 1.0.0-rc.1+build.123 | ✓matches |
| 0.0.1 | ✓matches |
| 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
- release tag validation
- package.json verification
- CI version-bump pre-check
- Docker tag pattern enforcement
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