Skip to content

PreRelease denotation regular expression (regex) #149

Closed

Description

@Oxymoron290

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

Activity

  1. Oxymoron290 commented on Aug 6, 2013

    @Oxymoron290
    Author

    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]))*$(?<!\.)$

  2. robsimmons commented on Aug 16, 2013

    @robsimmons

    -.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).

  3. TimLovellSmith commented on Oct 22, 2013

    @TimLovellSmith

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

  4. gvlx commented on Oct 5, 2014

    @gvlx

    This thread is still marked as open but it think it has been answered.
    A newer discussion on regex patterns follows on semver/semver.org#59

  5. added a commit that references this issue on Jul 10, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions