Skip to content

Instantly share code, notes, and snippets.

@simonwhatley
Last active March 18, 2019 12:28
Show Gist options
  • Save simonwhatley/354289d246362bc01cd25078be635e54 to your computer and use it in GitHub Desktop.
Save simonwhatley/354289d246362bc01cd25078be635e54 to your computer and use it in GitHub Desktop.

Semantic Version regex

Matches SemVer (Semantic Versioning) strings.

The string may start with a "v" but it is not matched.

/(?<=^[Vv]|^)(?:(?<major>(?:0|[1-9](?:(?:0|[1-9])+)*))[.](?<minor>(?:0|[1-9](?:(?:0|[1-9])+)*))[.](?<patch>(?:0|[1-9](?:(?:0|[1-9])+)*))(?:-(?<prerelease>(?:(?:(?:[A-Za-z]|-)(?:(?:(?:0|[1-9])|(?:[A-Za-z]|-))+)?|(?:(?:(?:0|[1-9])|(?:[A-Za-z]|-))+)(?:[A-Za-z]|-)(?:(?:(?:0|[1-9])|(?:[A-Za-z]|-))+)?)|(?:0|[1-9](?:(?:0|[1-9])+)*))(?:[.](?:(?:(?:[A-Za-z]|-)(?:(?:(?:0|[1-9])|(?:[A-Za-z]|-))+)?|(?:(?:(?:0|[1-9])|(?:[A-Za-z]|-))+)(?:[A-Za-z]|-)(?:(?:(?:0|[1-9])|(?:[A-Za-z]|-))+)?)|(?:0|[1-9](?:(?:0|[1-9])+)*)))*))?(?:[+](?<build>(?:(?:(?:[A-Za-z]|-)(?:(?:(?:0|[1-9])|(?:[A-Za-z]|-))+)?|(?:(?:(?:0|[1-9])|(?:[A-Za-z]|-))+)(?:[A-Za-z]|-)(?:(?:(?:0|[1-9])|(?:[A-Za-z]|-))+)?)|(?:(?:0|[1-9])+))(?:[.](?:(?:(?:[A-Za-z]|-)(?:(?:(?:0|[1-9])|(?:[A-Za-z]|-))+)?|(?:(?:(?:0|[1-9])|(?:[A-Za-z]|-))+)(?:[A-Za-z]|-)(?:(?:(?:0|[1-9])|(?:[A-Za-z]|-))+)?)|(?:(?:0|[1-9])+)))*))?)$/

Matches

Text Result
1.2.3 match
1.2.3-pre match
1.2.3+build match
1.2.3-pre+build match
v1.2.3 match
v1.2.3-pre match
v1.2.3+build match
v1.2.3-pre+build match
1.2 no match
1.2.x no match
1.x.x no match
1.2.3- no match
1.2.3+ no match

Match Groups

  • major – Major version number
  • minor – Minor version number
  • patch – Patch version number
  • prerelease – Prerelease version (example: alpha)
  • build – Build metadata
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment