T
ToolPrime

Regex for Semantic Version

Validates semantic versioning (semver) strings with optional pre-release and build metadata.

Pattern

/^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(-[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*)?(\+[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*)?$/

Live Tester

Enter a string to test

Examples

1.2.3
0.0.1
1.0.0-beta.1
1.2
v1.2.3
01.2.3

Pattern Breakdown

^(0|[1-9]\d*) — major version (no leading zeros)

\.(0|[1-9]\d*) — minor version

\.(0|[1-9]\d*) — patch version

(-[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*)? — optional pre-release identifiers

(\+[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*)?$ — optional build metadata

Code Snippets

Javascript

const regex = /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(-[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*)?(\+[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*)?$/;
regex.test("1.0.0-beta.1"); // true

Python

import re
pattern = r"^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(-[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*)?(\+[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*)?$"
bool(re.match(pattern, "1.0.0-beta.1"))  # True

Php

$pattern = '/^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(-[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*)?(\+[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*)?$/';
preg_match($pattern, "1.0.0-beta.1"); // 1

Related Patterns

Frequently Asked Questions

What is the regex for Semantic Version?
The regex pattern for Semantic Version is /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(-[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*)?(\+[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*)?$/. Validates semantic versioning (semver) strings with optional pre-release and build metadata.
How do I use the Semantic Version regex in JavaScript?
In JavaScript you create the pattern with a regex literal or the RegExp constructor and call .test() to check a string against it. A ready-to-copy example is shown in the Code Snippets section above, alongside Python and PHP versions.
What does the Semantic Version pattern match?
It matches strings like 1.2.3; it rejects strings like 1.2. See the Examples section above for the full list of matching and non-matching cases.

Related Tools