← Back to all regex patterns
Validation
Low backtracking risk

Regex for Semantic Version

Semantic version matching is useful in release tooling, changelog parsing, and dependency dashboards where you need to distinguish versions from free text.

Regex
/^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-[0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*)?(?:\+[0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*)?$/

Try this pattern

Matches current input
1.2.3

1 match found in the current text.

Passing examples

  • 1.2.3
  • 2.0.0-beta.1+build7

Failing examples

  • 01.2.3
  • v1.2

Code examples

JavaScript
const regex = /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-[0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*)?(?:\+[0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*)?$/g;
regex.test(input);
Python
import re
pattern = re.compile(r"^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-[0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*)?(?:\+[0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*)?$")
bool(pattern.search(input))
Go
re := regexp.MustCompile("^(0|[1-9]\\d*)\\.(0|[1-9]\\d*)\\.(0|[1-9]\\d*)(?:-[0-9A-Za-z-]+(?:\\.[0-9A-Za-z-]+)*)?(?:\\+[0-9A-Za-z-]+(?:\\.[0-9A-Za-z-]+)*)?$")
matched := re.MatchString(input)

Related Validation Patterns