Semantic version (semver) regex
Matches semantic versions with an optional leading v. Major, minor and patch are captured without leading zeros, then an optional pre-release after a hyphen and optional build metadata after a plus.
The pattern
//g
Sample text, 4 matches
1.0.0 and 2.14.3 are stable, 1.0.0-beta.1 is a pre-release, and 3.0.0+build.20240501 carries build metadata.
Point at a coloured group to see it in the pattern, the text and the explanation.
What it deliberately doesn’t do
- The pre-release and build parts are looser than the semver.org grammar allows.
- Ranges and operators (^1.2, ~1.2.3, >=2) are not versions and are not matched.
Explained, in order
\bWord boundary — asserts a boundary between a word character and a non-word character. Consumes no characters.vMatches the character 'v' literally?The preceding element matches optionally (zero or one time) (greedy)(0|[1-9]\d*)Group 1.Capturing group #1 — groups the pattern and captures the matched text for later reference0|[1-9]\d*Alternation — matches any ONE of the 2 alternatives listed above/below0Matches the character '0' literally[1-9]Character class — matches one character from: '1' through '9'\dMatches a digit (0–9)*The preceding element matches zero or more times (greedy)\.Matches the character '.' literally(0|[1-9]\d*)Group 2.Capturing group #2 — groups the pattern and captures the matched text for later reference0|[1-9]\d*Alternation — matches any ONE of the 2 alternatives listed above/below0Matches the character '0' literally[1-9]Character class — matches one character from: '1' through '9'\dMatches a digit (0–9)*The preceding element matches zero or more times (greedy)\.Matches the character '.' literally(0|[1-9]\d*)Group 3.Capturing group #3 — groups the pattern and captures the matched text for later reference0|[1-9]\d*Alternation — matches any ONE of the 2 alternatives listed above/below0Matches the character '0' literally[1-9]Character class — matches one character from: '1' through '9'\dMatches a digit (0–9)*The preceding element matches zero or more times (greedy)(?:-([\w.-]+))Non-capturing group — groups the pattern without capturing-Matches the character '-' literally([\w.-]+)Group 4.Capturing group #4 — groups the pattern and captures the matched text for later reference[\w.-]Character class — matches one character from: a word character (letter, digit, or underscore), '.', '-'+The preceding element matches one or more times (greedy)?The preceding element matches optionally (zero or one time) (greedy)(?:\+([\w.-]+))Non-capturing group — groups the pattern without capturing\+Matches the character '+' literally([\w.-]+)Group 5.Capturing group #5 — groups the pattern and captures the matched text for later reference[\w.-]Character class — matches one character from: a word character (letter, digit, or underscore), '.', '-'+The preceding element matches one or more times (greedy)?The preceding element matches optionally (zero or one time) (greedy)\bWord boundary — asserts a boundary between a word character and a non-word character. Consumes no characters.
In your language
JavaScript
The engine this page was tested with.
const re = /\bv?(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-([\w.-]+))?(?:\+([\w.-]+))?\b/g;
// Example
const matches = [...str.matchAll(/\bv?(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-([\w.-]+))?(?:\+([\w.-]+))?\b/g)];
for (const m of matches) {
console.log(m[0], m.index, m.groups);
}Python
Translated. Named groups are rewritten to (?P<name>…).
import re
pattern = re.compile(r"\bv?(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-([\w.-]+))?(?:\+([\w.-]+))?\b")
# Example
for m in pattern.finditer(text):
print(m.group(0), m.start(), m.groups())Go
Translated. Go's RE2 has no lookaround or backreferences.
package main
import (
"fmt"
"regexp"
)
var re = regexp.MustCompile(`\bv?(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-([\w.-]+))?(?:\+([\w.-]+))?\b`)
func main() {
for _, match := range re.FindAllStringSubmatch(text, -1) {
fmt.Println(match[0], match)
}
}Rust
Translated. The regex crate has no lookaround or backreferences.
use regex::Regex;
// Note: Rust finds all matches via find_iter / captures_iter (no 'g' flag).
fn main() {
let re = Regex::new(r#"\bv?(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-([\w.-]+))?(?:\+([\w.-]+))?\b"#).unwrap();
for m in re.find_iter(text) {
println!("{} at {}", m.as_str(), m.start());
}
}