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.
The following solution comes from @DavidFichtmueller from semver issue #232.
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-]+)*))?$
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-]+)*))?$
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}$
(?# 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-]+
)*
)
)?
$
Want to help fix a bug, create a test, or add a feature? Check out the Contributing Guidlines.
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.