Skip to content

Instantly share code, notes, and snippets.

@sharpjs
Last active May 22, 2018 21:42
Show Gist options
  • Save sharpjs/5482c67df9bda2a717979793c1e759a1 to your computer and use it in GitHub Desktop.
Save sharpjs/5482c67df9bda2a717979793c1e759a1 to your computer and use it in GitHub Desktop.
PowerShell/.NET Regular Expression for SemVer 2.0
# Pubic Domain. No rights reserved. No warranty.
# Reference:
# https://github.com/semver/semver/blob/master/semver.md
$VersionRegex = [regex] '(?nx)
^
(?<Version>
(?<Numbers>
( 0 | [1-9][0-9]* )
(
\.
( 0 | [1-9][0-9]* )
){2}
)
# Pre-release tag
(
-
( 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 metadata
(
\+
[0-9a-zA-Z-]+
(
\.
[0-9a-zA-Z-]+
)*
)?
)
$
'
@jwdonahue
Copy link

Your regex doesn't match a single valid version string out of 35.

See: https://regex101.com/r/wk7CDs/1

See also a valid regex and related discussion: semver/semver.org#59 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment