Skip to content

Semantic Versioning Regular Expressions - Grouping, Condesed, and POSIX

Notifications You must be signed in to change notification settings

T5CC/semver-regex

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

Semantic Versioning Regular Expressions - Grouping, Condesed, and POSIX

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published