[GH-ISSUE #199] How to verify that a string is a valid version? #7108

Closed
opened 2026-06-20 16:49:53 -05:00 by GiteaMirror · 3 comments
Owner

Originally created by @timfriske on GitHub (May 20, 2014).
Original GitHub issue: https://github.com/semver/semver/issues/199

Hi,

I've written the following regular expression to verify that a string is a valid version according to MAJOR.MINOR.PATCH-PREREL, i.e. excluding the build metadata part:

^(?!0\d)\d+(?:\.(?!0\d)\d+){2}(?:-(?:(?!0\d)(?:(?!_)[\w-])+|\.(?!\.|$))+)?$

I would appreciate it if this regular expression becomes part of the specification or at least the FAQ.

Originally created by @timfriske on GitHub (May 20, 2014). Original GitHub issue: https://github.com/semver/semver/issues/199 Hi, I've written the following regular expression to verify that a string is a valid version according to MAJOR.MINOR.PATCH-PREREL, i.e. excluding the build metadata part: ``` ^(?!0\d)\d+(?:\.(?!0\d)\d+){2}(?:-(?:(?!0\d)(?:(?!_)[\w-])+|\.(?!\.|$))+)?$ ``` I would appreciate it if this regular expression becomes part of the specification or at least the FAQ.
Author
Owner

@damianpowell commented on GitHub (May 20, 2014):

You could fairly trivially validate the build metadata too. It is part of the spec, after all.

^
(?!0\d)\d+(?:\.(?!0\d)\d+){2}
(?:-(?:(?!0\d)(?:(?!_)[\w-])+|\.(?!\.|$))+)?
(?:\+(?:(?:(?!_)[\w-])+|\.(?!\.|$))+)?
$

I included some line breaks for "readability" - these should obviously be removed if the expression is used in code.

<!-- gh-comment-id:43640395 --> @damianpowell commented on GitHub (May 20, 2014): You could fairly trivially validate the build metadata too. It is part of the spec, after all. ``` ^ (?!0\d)\d+(?:\.(?!0\d)\d+){2} (?:-(?:(?!0\d)(?:(?!_)[\w-])+|\.(?!\.|$))+)? (?:\+(?:(?:(?!_)[\w-])+|\.(?!\.|$))+)? $ ``` I included some line breaks for "readability" - these should obviously be removed if the expression is used in code.
Author
Owner

@FichteFoll commented on GitHub (May 20, 2014):

https://github.com/mojombo/semver/issues/192#issuecomment-40935547

<!-- gh-comment-id:43653930 --> @FichteFoll commented on GitHub (May 20, 2014): https://github.com/mojombo/semver/issues/192#issuecomment-40935547
Author
Owner

@haacked commented on GitHub (Jul 1, 2014):

Looks like this question was answered. If not, feel free to reopen.

<!-- gh-comment-id:47608370 --> @haacked commented on GitHub (Jul 1, 2014): Looks like this question was answered. If not, feel free to reopen.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/semver#7108