11,574 questions
You could just filter out the hyphens before doing the test:
string value = "FU48WYE8U878HW7";
if (Regex.IsMatch(value.Replace("-", ""), @"^[\w]{15}$")) {
Console.WriteLine("Matches");
}
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Im trying to get the regex expression that will match 15 characters excluding an optional hyphen. so that means the string could be 15 characters including a hyphen.
You could just filter out the hyphens before doing the test:
string value = "FU48WYE8U878HW7";
if (Regex.IsMatch(value.Replace("-", ""), @"^[\w]{15}$")) {
Console.WriteLine("Matches");
}