Regex to Match a Semantic Version
Copy the pattern, paste your version strings, and watch each valid semver light up. Below: how the pre-release and build parts work.
What this pattern matches
It matches a Semantic Versioning string: three dot-separated numbers, then an optional -prerelease tag and an optional +build tag.
Part by part
\d+\.\d+\.\d+ — MAJOR.MINOR.PATCH.
(?:-[0-9A-Za-z.-]+)? — an optional pre-release identifier like -rc.1 or -beta.
(?:\+[0-9A-Za-z.-]+)? — optional build metadata like +build.5.
Anchoring for validation
To validate a single field rather than scan text, wrap it in ^…$ so a leading v or trailing junk is rejected.
FAQ
Does it accept a leading v like v1.2.3?
No. Semver itself has no v prefix. Add v? before the pattern if your tooling uses tags like v1.2.3.
Are pre-release and build tags required?
No, both are optional groups, so a plain 1.2.3 matches.
How do I validate one string exactly?
Anchor it with ^ and $ so partial matches inside longer text are rejected.