Skip to content
Semantic Versioning Regular Expressions - Grouping, Condesed, and POSIX
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
BONUS.md
CONTRIBUTING.md
MISC.md
README.md
USAGE.md

README.md

Semantic Versioning v2.0.0 RegEx

This repo hosts RexEx solutions to check SemVer v2.0.0 compliance.

Note: These solutions are not canonical and are not guaranteed to capture every edge case.

Credit

The following solution comes from @DavidFichtmueller from semver issue #232.

Expressions

Without named capture groups

regex101 playground
^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$

With named capture groups

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

POSIX compliant

regex101 playground
^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(-((0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(\.(0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*)){0,1}(\+([0-9a-zA-Z-]+(\.[0-9a-zA-Z-]+)*)){0,1}$

Breakdown

(?# semantic versioning 2.0.0 regex matching )
^
    (?# First capture group of MAJOR.MINOR.PATCH )
    (?<major>
        0|[1-9]\d*
    )
    \.
    (?<minor>
        0|[1-9]\d*
    )
    \.
    (?<patch>
        0|[1-9]\d*
    )
    (?# Capture Prerelease, if it exists )
    (?:-
        (?<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-]*
                )
            )*
        )
    )?
    (?# Capture metadata, if it exists)
    (?:
        \+
        (?<buildmetadata>
            [0-9a-zA-Z-]+
            (?:
                \.[0-9a-zA-Z-]+
            )*
        )
    )?
$

Contributing

Want to help fix a bug, create a test, or add a feature? Check out the Contributing Guidlines.

Addenum

This repo was born out of semver/semvor.org Issue Thread #59
The regex here reflects work that has been done by @gvlx and @jwdonahue.

Looking for some other fun stuff? Check out the Bonus Document and Miscellanous Document.

You can’t perform that action at this time.