Git Product home page Git Product logo

bash-semver-regex's Introduction

Bash-compatible SemVer Regex

A regular expression for use with bash that captures the fragments of the Semantic Versioning Specification. This repo also contains a short bash script that runs the regex against a set of example version numbers to check its validity.

This uses bash built-in regex handling, which uses POSIX regex. Think of it as an extension to the suggested regex FAQ entry for use in bash (which doesn't have a concept of non-capturing groups).

The Regex

Terse:

^(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)(-((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-]*))*))?(\+([0-9a-zA-Z-]+(\.[0-9a-zA-Z-]+)*))?$

Human-Readable:

# Regex for a semver digit
D='0|[1-9][0-9]*'
# Regex for a semver pre-release word
PW='[0-9]*[a-zA-Z-][0-9a-zA-Z-]*'
# Regex for a semver build-metadata word
MW='[0-9a-zA-Z-]+'

if [[ "$INPUT" =~ ^($D)\.($D)\.($D)(-(($D|$PW)(\.($D|$PW))*))?(\+($MW(\.$MW)*))?$ ]]; then
    MAJOR="${BASH_REMATCH[1]}"
    MINOR="${BASH_REMATCH[2]:-""}"
    PATCH="${BASH_REMATCH[3]:-""}"
    PRE_RELEASE="${BASH_REMATCH[5]:-""}"
    BUILD_METADATA="${BASH_REMATCH[10]:-""}"
fi

The Capture Groups

The individual parts are captured as follows:

  • Capture group 1: Major
  • Capture group 2: Minor
  • Capture group 3: Patch
  • Capture group 5: Prerelease
  • Capture group 10: Build metadata

Original Source

This repo came to life following a discussion in an issue in the semver repo on GitHub. See here.

bash-semver-regex's People

Contributors

har7an avatar

Stargazers

mordek avatar Dmitry Tavern avatar Mirko Jechow avatar Walter avatar Joseph Donahue avatar

Watchers

Joseph Donahue avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.