PreRelease denotation regular expression (regex) #96

Closed
opened 2026-02-17 11:13:20 -06:00 by GiteaMirror · 4 comments
Owner

Originally created by @Oxymoron290 on GitHub (Aug 6, 2013).

I would like to verify my regex here is correct according to number 9 on the SemVer Specification.
^([-a-zA-Z0-9])([-a-zA-Z0-9\.](?<!\.{2}))*$(?<!\.)$

Here are some tests I ran using C#.NET

            var regex = new Regex(@"^([-])([-a-zA-Z0-9\.](?<!\.{2})(?<!\.0[0-9]))*$(?<!\.)$");

            #region Test Strings
            var tests = new List<string>
                {
                    "-alpha",
                    "-beta",
                    "-alpha.beta",
                    "-alpha.beta.1",
                    "-alpha.1",
                    "-alpha0.valid",
                    "-alpha.0valid",
                    "Above-Me-Is-All-Valid",
                    "-invalid.01",
                    "alpha",
                    "alpha.beta",
                    "alpha.beta.1",
                    "alpha.1",
                    "alpha+beta",
                    "alpha_beta",
                    "alpha.",
                    "alpha..",
                    "beta\"",
                    "=Aplay",
                    "-alpha+beta",
                    "-alpha_beta",
                    "-alpha.",
                    "-alpha..",
                    "-alpha..1",
                    "-alpha...1",
                    "-alpha....1",
                    "-alpha.....1",
                    "-alpha......1",
                    "-alpha.......1",
                };
            #endregion

            var result = "";
            foreach (var test in tests)
            {
                if (regex.IsMatch(test))
                {
                    result += test + ": Valid\n";
                }
                else
                {
                    result += test + ": Invalid\n";
                }
            }

            Assert.IsNotNull(result);

and it's output:

             -alpha: Valid
             -beta: Valid
             -alpha.beta: Valid
             -alpha.beta.1: Valid
             -alpha.1: Valid
             -alpha0.valid: Valid
             -alpha.0valid: Valid

             Above-Me-Is-All-Valid: Invalid
             -invalid.01: Invalid
             alpha.beta: Invalid
             alpha.beta.1: Invalid
             alpha.1: Invalid
             alpha+beta: Invalid
             alpha_beta: Invalid
             alpha.: Invalid
             alpha..: Invalid
             beta\": Invalid
             =Aplay: Invalid
             -alpha+beta: Invalid
             -alpha_beta: Invalid
             -alpha.: Invalid
             -alpha..: Invalid
             -alpha..1: Invalid
             -alpha...1: Invalid
             -alpha....1: Invalid
             -alpha.....1: Invalid
             -alpha......1: Invalid
             -alpha.......1: Invalid
Originally created by @Oxymoron290 on GitHub (Aug 6, 2013). I would like to verify my regex here is correct according to number 9 on the SemVer Specification. `^([-a-zA-Z0-9])([-a-zA-Z0-9\.](?<!\.{2}))*$(?<!\.)$` Here are some tests I ran using C#.NET ``` var regex = new Regex(@"^([-])([-a-zA-Z0-9\.](?<!\.{2})(?<!\.0[0-9]))*$(?<!\.)$"); #region Test Strings var tests = new List<string> { "-alpha", "-beta", "-alpha.beta", "-alpha.beta.1", "-alpha.1", "-alpha0.valid", "-alpha.0valid", "Above-Me-Is-All-Valid", "-invalid.01", "alpha", "alpha.beta", "alpha.beta.1", "alpha.1", "alpha+beta", "alpha_beta", "alpha.", "alpha..", "beta\"", "=Aplay", "-alpha+beta", "-alpha_beta", "-alpha.", "-alpha..", "-alpha..1", "-alpha...1", "-alpha....1", "-alpha.....1", "-alpha......1", "-alpha.......1", }; #endregion var result = ""; foreach (var test in tests) { if (regex.IsMatch(test)) { result += test + ": Valid\n"; } else { result += test + ": Invalid\n"; } } Assert.IsNotNull(result); ``` and it's output: ``` -alpha: Valid -beta: Valid -alpha.beta: Valid -alpha.beta.1: Valid -alpha.1: Valid -alpha0.valid: Valid -alpha.0valid: Valid Above-Me-Is-All-Valid: Invalid -invalid.01: Invalid alpha.beta: Invalid alpha.beta.1: Invalid alpha.1: Invalid alpha+beta: Invalid alpha_beta: Invalid alpha.: Invalid alpha..: Invalid beta\": Invalid =Aplay: Invalid -alpha+beta: Invalid -alpha_beta: Invalid -alpha.: Invalid -alpha..: Invalid -alpha..1: Invalid -alpha...1: Invalid -alpha....1: Invalid -alpha.....1: Invalid -alpha......1: Invalid -alpha.......1: Invalid ```
Author
Owner

@Oxymoron290 commented on GitHub (Aug 6, 2013):

also, would a pre-release in the format of -.alpha be valid? I'm guessing not due to an empty identifier, however is the first alphanumeric string immediately after the hyphen an identifier?

Additionally according to the specification a pre-release in the format of --alpha.beta would be legal. Is this intended? It's strange but it appears to be allowed. Please correct me if I'm wrong on that.

If not I suppose the expression could be updated to ^([-](?!\.))([-a-zA-Z0-9\.](?<!\.{2})(?<!\.0[0-9]))*$(?<!\.)$

@Oxymoron290 commented on GitHub (Aug 6, 2013): also, would a pre-release in the format of `-.alpha` be valid? I'm guessing not due to an empty identifier, however is the first alphanumeric string immediately after the hyphen an identifier? Additionally according to the specification a pre-release in the format of `--alpha.beta` would be legal. Is this intended? It's strange but it appears to be allowed. Please correct me if I'm wrong on that. If not I suppose the expression could be updated to `^([-](?!\.))([-a-zA-Z0-9\.](?<!\.{2})(?<!\.0[0-9]))*$(?<!\.)$`
Author
Owner

@robsimmons commented on GitHub (Aug 16, 2013):

-.alpha shouldn't be valid, periods aren't identifiers so I'm not sure what you'd even consider the first alphanumeric string after the hyphen to be. Issue #147 talks about the leading hyphen issue (and references that it's been discussed before).

@robsimmons commented on GitHub (Aug 16, 2013): `-.alpha` shouldn't be valid, periods aren't identifiers so I'm not sure what you'd even consider the first alphanumeric string after the hyphen to be. Issue #147 talks about the leading hyphen issue (and references that it's been discussed before).
Author
Owner

@TimLovellSmith commented on GitHub (Oct 22, 2013):

-.alpha would be a violation of 'identifiers must NOT be empty'

@TimLovellSmith commented on GitHub (Oct 22, 2013): -.alpha would be a violation of 'identifiers must NOT be empty'
Author
Owner

@gvlx commented on GitHub (Oct 5, 2014):

This thread is still marked as open but it think it has been answered.
A newer discussion on regex patterns follows on https://github.com/mojombo/semver.org/issues/59

@gvlx commented on GitHub (Oct 5, 2014): This thread is still marked as open but it think it has been answered. A newer discussion on regex patterns follows on https://github.com/mojombo/semver.org/issues/59
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/semver#96