BytePane

Semantic Version Regex Pattern

Validates semantic version strings (SemVer) with major.minor.patch and optional pre-release and build metadata.

</>
Code & Programming
Intermediate
Difficulty
Universal
Language
none
Flags
// Regular Expression
/^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(-[a-zA-Z0-9.]+)?(\+[a-zA-Z0-9.]+)?$/

Live Regex Tester

Pattern Breakdown

^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(-[a-zA-Z0-9.]+)?(\+[a-zA-Z0-9.]+)?$
Character class [ ]
Group ( )
Quantifier { }
Anchor ^ $
Repetition * + ?
Escape \
Alternation |
Any char .

Code Examples

JavaScript

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

// Extract matches
const matches = test.match(regex);
console.log(matches);

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.]+)?$'
test = "2.1.0-beta.1"
match = re.search(pattern, test)
print(match)  # Found!

Go

package main

import (
    "fmt"
    "regexp"
)

func main() {
    re := regexp.MustCompile(`^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(-[a-zA-Z0-9.]+)?(\+[a-zA-Z0-9.]+)?$`)
    fmt.Println(re.MatchString("2.1.0-beta.1")) // true
}

Common Use Cases

Package managementCI/CDrelease automation

Match Examples

InputResult
2.1.0-beta.1Match
1.2No Match

About the Semantic Version Regex

Validates semantic version strings (SemVer) with major.minor.patch and optional pre-release and build metadata.

Regular expressions (regex) are powerful pattern matching tools used across virtually all programming languages. The semantic version pattern is classified as intermediate difficulty in the code & programming category. It works in all major programming languages.

When using this regex, always consider edge cases and test thoroughly with real-world data. Use the interactive tester above to validate the pattern against your specific inputs before deploying to production.

Need More Regex Patterns?

Browse our complete library of 100+ regex patterns with interactive testers.

Frequently Asked Questions

What is the Semantic Version regex pattern?

Validates semantic version strings (SemVer) with major.minor.patch and optional pre-release and build metadata.

How do I use the Semantic Version regex?

Use the pattern /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(-[a-zA-Z0-9.]+)?(\+[a-zA-Z0-9.]+)?$/ in your code. In JavaScript: new RegExp('^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(-[a-zA-Z0-9.]+)?(\+[a-zA-Z0-9.]+)?$', ''). Test it above with your own input.

What does this Semantic Version regex match?

This pattern matches: "2.1.0-beta.1". It does NOT match: "1.2". Package management, CI/CD, release automation.

Is the Semantic Version regex beginner-friendly?

This pattern is rated Intermediate. It uses some advanced features like character classes and quantifiers.

What languages support the Semantic Version regex?

This pattern works in all major programming languages including JavaScript, Python, Java, C#, Go, Ruby, PHP, and more. Syntax may vary slightly between regex engines.

Can I modify the Semantic Version regex for my use case?

Yes! Use the interactive tester above to modify the pattern and test with your own data. Common modifications include making it case-insensitive (add 'i' flag), matching globally (add 'g' flag), or adjusting character classes.

Related Tools