Match a regular expression

George Bush 20 Reputation points
2024-02-22T14:57:27.35+00:00

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.

Developer technologies | C#
0 comments No comments
{count} votes

Accepted answer
  1. P a u l 10,761 Reputation points
    2024-02-22T15:10:26.08+00:00

    You could just filter out the hyphens before doing the test:

    string value = "FU48WYE8U878HW7";
    
    if (Regex.IsMatch(value.Replace("-", ""), @"^[\w]{15}$")) {
        Console.WriteLine("Matches");
    }
    

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.