^(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-]+)*))?$ This is the official Semantic Versioning 2.0.0 pattern, matching MAJOR.MINOR.PATCH with optional pre-release and build-metadata suffixes. It forbids leading zeros in numeric identifiers and enforces the allowed character sets. Identifier patterns recognize structured tokens such as UUIDs, hashes, Base64 strings and version numbers. These formats have exact layouts and alphabets defined by specifications, so the regex encodes group lengths, separators and allowed characters. A match confirms the shape; semantic validity (a real version, a correct checksum) usually needs an additional library check. It needs no special flags. Apply it to the whole input as a format contract. It rejects a leading v (v1.2.3); strip the prefix before matching if your tags include it. Pre-release numeric identifiers must not have leading zeros (1.2.3-01 is invalid), which the pattern enforces. Build metadata is ignored for precedence comparisons — two versions differing only in build metadata are considered equal. As with any regular expression, confirm its behavior in your target engine, test boundary and adversarial inputs, and watch for catastrophic backtracking before relying on it in production.
Token-by-token explanation
(0|[1-9]\d*) — a numeric identifier without leading zeros (repeated for major/minor/patch)\.(...)\.(...) — dot-separated core version(?:-((?:...)))? — optional pre-release: dot-separated identifiers after a hyphen(?:\+([0-9a-zA-Z-]+...))? — optional build metadata after a plus[a-zA-Z-] — pre-release identifiers may contain letters and hyphens
What it matches
1.0.02.3.4-alpha.11.0.0-beta+exp.sha.5114f850.1.010.20.30 What it does not match
1.001.2.31.2.3-011.2.3+v1.2.3 When to use it
Reach for an identifier pattern when you must recognize a standardized token — a UUID, a hash, a Base64 blob or a version string — before decoding or looking it up. The pattern confirms the layout and alphabet defined by the specification, which catches typos and wrong formats early. Follow it with a library that decodes or verifies semantics, since a correctly shaped identifier may still be invalid, expired or refer to nothing in your system.
Caveats
- It rejects a leading v (v1.2.3); strip the prefix before matching if your tags include it.
- Pre-release numeric identifiers must not have leading zeros (1.2.3-01 is invalid), which the pattern enforces.
- Build metadata is ignored for precedence comparisons — two versions differing only in build metadata are considered equal.
Code examples
const re = /^(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-]+)*))?$/;
const input = "1.0.0";
if (re.test(input)) {
// valid: proceed
} else {
// reject with a helpful message
} import re
rx = re.compile(r"^(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-]+)*))?$")
value = "1.0.0"
if rx.fullmatch(value):
pass # valid
else:
raise ValueError("invalid format") import java.util.regex.*;
Pattern p = Pattern.compile("^(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-]+)*))?$");
Matcher m = p.matcher("1.0.0");
boolean ok = m.matches(); // whole input must conform <?php
$subject = "1.0.0";
if (preg_match('/^(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-]+)*))?$/', $subject, $m)) {
// $m[0] is the full match
} How to test it
Before trusting any pattern, test it against three kinds of input: valid examples that must match, invalid examples that must be rejected, and edge cases at the boundaries (empty strings, maximum lengths, unusual separators and Unicode). Paste the expression into a live regex tester with your actual sample data and confirm the highlighted matches are exactly what you intend. Watch for catastrophic backtracking by trying long non-matching inputs, and remember that engine differences (JavaScript, Python, Java, PCRE, Go/RE2) mean a pattern that works in one may fail or behave differently in another — especially lookarounds, which Go's RE2 does not support at all.
Frequently asked questions
What is the difference between pre-release and build metadata?
Pre-release (after -) denotes an unstable version that sorts before the release; build metadata (after +) is extra info ignored when comparing precedence.
Why are leading zeros forbidden?
SemVer specifies numeric identifiers must not have leading zeros to keep ordering unambiguous, so 01.2.3 and 1.2.3-01 are invalid.
Does a matching identifier guarantee it is real?
No — it confirms the textual shape only. A UUID-shaped string may not be a generated UUID, and a Base64-shaped string may decode to nonsense. Use a library to decode, verify checksums or confirm semantics.
What does the +json or +xml suffix mean?
It is the structured-syntax suffix convention (RFC 6839): application/geo+json is JSON with a defined structure. It tells clients the underlying serialization while naming the specific format.
Try the Regex Tester
Live regex matching with groups, flags and explanations.