Regular Expressions 101

Community Patterns

Community Library Entry

4

Regular Expression
Recommended
PCRE (PHP <7.3)

/
^(?'MAJOR'0|(?:[1-9]\d*))\.(?'MINOR'0|(?:[1-9]\d*))\.(?'PATCH'0|(?:[1-9]\d*))(?:-(?'prerelease'(?:0|(?:[1-9A-Za-z-][0-9A-Za-z-]*))(?:\.(?:0|(?:[1-9A-Za-z-][0-9A-Za-z-]*)))*))?(?:\+(?'build'(?:0|(?:[1-9A-Za-z-][0-9A-Za-z-]*))(?:\.(?:0|(?:[1-9A-Za-z-][0-9A-Za-z-]*)))*))?$
/
gm

Description

Semantic Versioning regex pattern according to Tom Preston-Werner

http://semver.org/ https://github.com/mojombo/semver/blob/master/semver.md

First version of this regex follows semver 2.0.0.

Discussion of semver and this regex was posted on https://github.com/mojombo/semver.org/issues/59

Submitted by @gvlx <Gerardo Lisboa> - 10 years ago