Regex and cron

Regex for Semantic Versioning (SemVer)

Parse version strings exactly as the Semantic Versioning 2.0.0 spec defines them.

  • Free foreverNo trial, no paywall, no usage limits.
  • No sign-upOpen the page and start. We never ask for an email to use a tool.
  • PrivateEverything runs in your browser. Your input is never uploaded or stored on our servers.
  • Works offlineOnce the page has loaded, the tool keeps working without a connection.

Pattern

/^(?<major>0|[1-9]\d*)\.(?<minor>0|[1-9]\d*)\.(?<patch>0|[1-9]\d*)(?:-(?<prerelease>(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+(?<build>[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$/

Regular expression

JavaScript syntax, run by your browser's own engine in a background worker. Press / anywhere to jump here.
Reset to the original pattern and examplesShow cheat sheet: click any token to insert itCopy pattern
Find every match instead of stopping at the first. Needed for matchAll() and for replacing all occurrences.Case-insensitive: /abc/i also matches "ABC" and "aBc".^ and $ match at the start and end of every line, not just the whole string. Use it to test one value per line.Lets . match line breaks too. Without it, . stops at \n and \r.Treats the pattern as Unicode code points: enables \p{…} property escapes and \u{…}, makes emoji count as one character, and rejects sloppy escapes.Only matches starting exactly at lastIndex (position 0 here), never further along. Useful for tokenizers that consume input piece by piece.Records the start and end index of every capture group (match.indices). The tester always shows group positions, so this only changes your code.The upgraded unicode mode (ES2024): set operations like [\p{L}--[a-z]] and [A&&B], string properties like \p{RGI_Emoji}. Cannot be combined with u.
8 matches8 matches · 5 groups

Test string

Matches are highlighted as you type, in alternating colors. Click inside a match (or hover its row below) to color its capture groups.
8 matchesClear test string

Explanation

Every part of the pattern in plain English. Hover a row to highlight that part of the pattern.
Copy explanation

Loading the explainer…

Every match with its position and capture groups.Preview a replacement with $1, $<name> and $& references, then copy the result.Copy a ready-to-run snippet for JavaScript, Python, Go, C#, PHP or Dart.

Matches

Named groups are shown by name. undefined means the group did not take part in that match.
Copy matches as JSONDownload matches.csv
#MatchPositionGroups
11.0.00–5
  • major1
  • minor0
  • patch0
  • prereleaseundefined
  • buildundefined
22.10.36–12
  • major2
  • minor10
  • patch3
  • prereleaseundefined
  • buildundefined
30.1.013–18
  • major0
  • minor1
  • patch0
  • prereleaseundefined
  • buildundefined
41.0.0-alpha19–30
  • major1
  • minor0
  • patch0
  • prereleasealpha
  • buildundefined
51.0.0-alpha.131–44
  • major1
  • minor0
  • patch0
  • prereleasealpha.1
  • buildundefined
61.0.0-0.3.745–56
  • major1
  • minor0
  • patch0
  • prerelease0.3.7
  • buildundefined
71.0.0+2013031314470057–77
  • major1
  • minor0
  • patch0
  • prereleaseundefined
  • build20130313144700
81.0.0-beta+exp.sha.5114f8578–104
  • major1
  • minor0
  • patch0
  • prereleasebeta
  • buildexp.sha.5114f85

Need a secure web app built around this? Talk to our team

Quick answer

The official semantic versioning regex, published on semver.org, matches major.minor.patch with an optional -prerelease and +build part, and captures each piece in a named group. Numbers cannot have leading zeros, so 1.0.0, 2.10.3, 1.0.0-alpha.1 and 1.0.0+20130313144700 match, while 1.0, 01.0.0, v1.0.0 and 1.0.0-01 do not. Strip a leading v before matching.

  • Tested examples
  • Token-by-token breakdown
  • Code in 5 languages
  • Live tester

How to use the semantic version regex

  1. 1Copy the pattern with Copy pattern in the tester, or take a ready-made snippet for your language from the code section below.
  2. 2Check the sample lines in Test string: every line the semantic version regex accepts is highlighted, rejected lines stay plain.
  3. 3Paste your own values one per line to see which pass, and read Explanation for what each part of the pattern does.
  4. 4Open the Code tab for JavaScript, Python, Go, C#, PHP or Dart with the pattern translated and escaped.
  5. 5Validate again on the server: client-side checks give users quick feedback, not security.

How does the semantic version regex work?

Read it left to right. Each piece below matches one part of the input:

  • (?<major>0|[1-9]\d*) The major version: 0, or a number without a leading zero.
  • \.(?<minor>0|[1-9]\d*) A dot and the minor version, same rule.
  • \.(?<patch>0|[1-9]\d*) A dot and the patch version.
  • (?:-(?<prerelease>…))? Optional prerelease after a hyphen: dot-separated identifiers. Numeric ones cannot have leading zeros (alpha.1, rc.2, 0.3.7).
  • (?:\+(?<build>[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))? Optional build metadata after a plus: dot-separated alphanumerics. Leading zeros are fine here.
  • ^ … $ Anchored, so nothing can come before or after the version.

Which strings does the semantic version regex match?

The whole input must match, from the first character to the last. Every row below is checked against the pattern by our automated test suite before each release.

InputMatches?Why
1.0.0YesA release.
2.10.3YesMulti-digit numbers.
0.1.0YesMajor version zero, for initial development.
1.0.0-alphaYesA prerelease.
1.0.0-alpha.1YesPrerelease with a numeric identifier.
1.0.0-0.3.7YesAll-numeric prerelease identifiers.
1.0.0+20130313144700YesBuild metadata only.
1.0.0-beta+exp.sha.5114f85YesPrerelease and build metadata.
1.0NoAll three numbers are required.
01.0.0NoNo leading zeros in version numbers.
v1.0.0NoA v prefix is a tag convention, not part of SemVer. Strip it first.
1.0.0-01NoNumeric prerelease identifiers cannot have leading zeros.
1.0.0-NoAn empty prerelease.
1.2.3.4NoFour numbers.

How do I compare two semantic versions?

Not with string comparison: "1.10.0" < "1.9.0" is true alphabetically. Compare the captured numbers in order, then apply the prerelease rule from Semantic Versioning 2.0.0: a version with a prerelease is lower than the same version without one, and prerelease identifiers compare numerically when both are numbers and alphabetically otherwise. So:

1.0.0-alpha < 1.0.0-alpha.1 < 1.0.0-beta < 1.0.0-rc.1 < 1.0.0

Build metadata (+…) is ignored when comparing. In practice, use a library that has these rules right: semver on npm, packaging.version in Python, golang.org/x/mod/semver in Go.

Why does this pattern use named groups?

So the code that reads it says what it means: match.groups.major instead of match[1]. It is the same regex semver.org publishes, which makes it easy to verify. The code export translates the groups for each language: Python and Go use (?P<major>…), and PHP refers to them by number in replacements.

To accept a leading v, as in Git tags, add v? after the ^. For npm-style ranges (^1.2.0, ~1.2), do not extend the regex: those are a separate grammar, and the semver package parses them.

How do I use the semantic version regex in JavaScript, Python, Go, C# and PHP?

The pattern is translated for each engine rather than pasted as is: named groups, anchors, escapes, shorthand classes and flags are adjusted so each snippet behaves as closely to the JavaScript version as its engine allows. Differences that remain (Go's RE2 has no lookarounds or backreferences, for example) are called out under each one. The Code tab in the tester above also exports Dart, and updates as you edit the pattern.

JavaScript and TypeScript

const regex = /^(?<major>0|[1-9]\d*)\.(?<minor>0|[1-9]\d*)\.(?<patch>0|[1-9]\d*)(?:-(?<prerelease>(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+(?<build>[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$/;

function isValid(value) {
  return regex.test(value);
}

console.log(isValid("1.0.0"));

Python

import re

pattern = re.compile(r"^(?P<major>0|[1-9][0-9]*)\.(?P<minor>0|[1-9][0-9]*)\.(?P<patch>0|[1-9][0-9]*)(?:-(?P<prerelease>(?:0|[1-9][0-9]*|[0-9]*[a-zA-Z\-][0-9a-zA-Z\-]*)(?:\.(?:0|[1-9][0-9]*|[0-9]*[a-zA-Z\-][0-9a-zA-Z\-]*))*))?(?:\+(?P<build>[0-9a-zA-Z\-]+(?:\.[0-9a-zA-Z\-]+)*))?\Z")

def is_valid(value: str) -> bool:
    return pattern.search(value) is not None

print(is_valid("1.0.0"))
  • \d and \w are written as [0-9] and [A-Za-z0-9_] because in Python they also match digits and letters from other scripts.
  • JavaScript's $ only matches at the very end of the string. Python's $ also matches before a trailing newline, so the pattern uses \Z instead.

Go

package main

import (
	"fmt"
	"regexp"
)

func main() {
	re := regexp.MustCompile(`^(?P<major>0|[1-9]\d*)\.(?P<minor>0|[1-9]\d*)\.(?P<patch>0|[1-9]\d*)(?:-(?P<prerelease>(?:0|[1-9]\d*|\d*[a-zA-Z\-][0-9a-zA-Z\-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z\-][0-9a-zA-Z\-]*))*))?(?:\+(?P<build>[0-9a-zA-Z\-]+(?:\.[0-9a-zA-Z\-]+)*))?$`)
	fmt.Println(re.MatchString("1.0.0"))
}

C#

using System;
using System.Text.RegularExpressions;

// The timeout stops a pathological input from hanging the thread (ReDoS).
var regex = new Regex(@"^(?<major>0|[1-9][0-9]*)\.(?<minor>0|[1-9][0-9]*)\.(?<patch>0|[1-9][0-9]*)(?:-(?<prerelease>(?:0|[1-9][0-9]*|[0-9]*[a-zA-Z\-][0-9a-zA-Z\-]*)(?:\.(?:0|[1-9][0-9]*|[0-9]*[a-zA-Z\-][0-9a-zA-Z\-]*))*))?(?:\+(?<build>[0-9a-zA-Z\-]+(?:\.[0-9a-zA-Z\-]+)*))?\z", RegexOptions.None, TimeSpan.FromSeconds(1));
Console.WriteLine(regex.IsMatch("1.0.0"));
  • \d and \w are written as [0-9] and [A-Za-z0-9_] because in .NET they also match digits and letters from other scripts.
  • JavaScript's $ only matches at the very end of the string. .NET's $ also matches before a trailing newline, so the pattern uses \z instead.

PHP

<?php

$pattern = '/^(?<major>0|[1-9]\d*)\.(?<minor>0|[1-9]\d*)\.(?<patch>0|[1-9]\d*)(?:-(?<prerelease>(?:0|[1-9]\d*|\d*[a-zA-Z\-][0-9a-zA-Z\-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z\-][0-9a-zA-Z\-]*))*))?(?:\+(?<build>[0-9a-zA-Z\-]+(?:\.[0-9a-zA-Z\-]+)*))?$/D';

function isValid(string $value, string $pattern): bool
{
    return preg_match($pattern, $value) === 1;
}

var_dump(isValid('1.0.0', $pattern));
  • The D modifier makes $ match only at the very end, like JavaScript (PCRE's $ also matches before a trailing newline).

Browse all 25 patterns in the regex pattern library, or build your own in the regex tester.

Frequently asked questions

No. The specification defines the version as 1.0.0; the v is a common prefix for Git tags. Strip it before validating, or add v? after the ^ if you want to accept it.
Major, minor, patch and numeric prerelease identifiers cannot. Build metadata can, because it is not used for ordering.
It is the regular expression suggested in the Semantic Versioning 2.0.0 specification FAQ on semver.org, using named capture groups for each part.

Last updated by the Playxoft engineering team.

From the engineers who built this tool

Building something bigger than a quick fix?

Auth, payments, dashboards and security are where web apps get hard. We have shipped them many times. Share your idea and we will map out a realistic plan and estimate, free and with no obligation.

More free tools

See all tools

Regex Tester

Test regex live with highlighted matches, plain-English explanations and code export.

Open tool

Cron Expression Generator

New

Build and explain cron schedules, see the next runs in any time zone, copy platform snippets.

Open tool

SQL Formatter

Beautify or minify SQL in 12 dialects, with keyword case, indent and line:column errors.

Open tool

cURL to Python Converter

New

Convert cURL commands into Python requests or httpx code with params, json, files and auth.

Open tool

cURL to Fetch Converter

New

Turn any cURL command into JavaScript fetch() code with headers, JSON, forms and auth.

Open tool

cURL to Axios Converter

New

Convert cURL commands into an Axios request config with params, data, auth and timeouts.

Open tool