When provided an input like a formatted date, this library incorrectly parses it as semver.
package main
import (
"fmt"
"regexp"
)
const (
VersionRegexpRaw string = `v?([0-9]+(\.[0-9]+)*?)` +
`(-([0-9]+[0-9A-Za-z\-~]*(\.[0-9A-Za-z\-~]+)*)|(-?([A-Za-z\-~]+[0-9A-Za-z\-~]*(\.[0-9A-Za-z\-~]+)*)))?` +
`(\+([0-9A-Za-z\-~]+(\.[0-9A-Za-z\-~]+)*))?` +
`?`
// SemverRegexpRaw requires a separator between version and prerelease
SemverRegexpRaw string = `v?([0-9]+(\.[0-9]+)*?)` +
`(-([0-9]+[0-9A-Za-z\-~]*(\.[0-9A-Za-z\-~]+)*)|(-([A-Za-z\-~]+[0-9A-Za-z\-~]*(\.[0-9A-Za-z\-~]+)*)))?` +
`(\+([0-9A-Za-z\-~]+(\.[0-9A-Za-z\-~]+)*))?` +
`?`
)
// mostly from https://semver.org, but v added in front for npm compatibility
const validatingRE = `^[v]?(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-]+)*))?$`
var semverOrgMatcher = regexp.MustCompile(validatingRE)
func main() {
version := "2020-02-26T7-36-46"
versionRegexp := regexp.MustCompile("^" + VersionRegexpRaw + "$")
semverRegexp := regexp.MustCompile("^" + SemverRegexpRaw + "$")
fmt.Println(versionRegexp.FindStringSubmatch(version))
fmt.Println(semverRegexp.FindStringSubmatch(version))
fmt.Println("is semver according to semver.org:", semverOrgMatcher.MatchString(version))
}
Go playground
I would have expected this to return an error about a malformed version rather than [2020-02-26T7-36-46 2020 -02-26T7-36-46 02-26T7-36-46 ]
When provided an input like a formatted date, this library incorrectly parses it as semver.
Go playground
I would have expected this to return an error about a malformed version rather than
[2020-02-26T7-36-46 2020 -02-26T7-36-46 02-26T7-36-46 ]