regexhelper
Test a pattern. Copy a ready-made one. No signup.

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.

Regex pattern

/\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?(?:\+[0-9A-Za-z.-]+)?/g

This matches a SemVer version like 1.2.3, 1.0.0-alpha.1 or 1.0.0+build.5, with optional pre-release and build metadata. It follows the semver.org shape but does not enforce every numeric-identifier rule.

/ /

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.

Use it in your code

Python
import re
text = "Releases: 1.2.3, 2.0.0-rc.1, 1.0.0+build.5, 10.4.11-beta. Not semver: 1.2 or v3."
pattern = r'''\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?(?:\+[0-9A-Za-z.-]+)?'''
for m in re.finditer(pattern, text):
    print(m.group())
JavaScript
const text = "Releases: 1.2.3, 2.0.0-rc.1, 1.0.0+build.5, 10.4.11-beta. Not semver: 1.2 or v3.";
const re = new RegExp(String.raw`\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?(?:\+[0-9A-Za-z.-]+)?`, "g");
console.log(text.match(re));

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.