Repository navigation
Closed
Description
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
Metadata
Metadata
Assignees
Labels
No labels
Activity
Oxymoron290 commentedon Aug 6, 2013
also, would a pre-release in the format of
-.alphabe 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.betawould 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]))*$(?<!\.)$robsimmons commentedon Aug 16, 2013
-.alphashouldn'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).TimLovellSmith commentedon Oct 22, 2013
-.alpha would be a violation of 'identifiers must NOT be empty'
gvlx commentedon 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 semver/semver.org#59
Merge pull request semver#149 from magol/patch-1