Question

I would love to accept the following formats:

"23:59", "2:3:04", "02:00:09", "23:07:00"

with this regex pattern:

return Regex.IsMatch(timeIn, @"^([0-2]?[0-9]:[0-5]?[0-9])|([0-2]?[0-9]:[0-5]?[0-9]:[0-5]?[0-9])$") ? true : false;

unfortunately it accepts other formats as well, e.g.: 00:00:99

what am I doing wrong? Thank you.

Was it helpful?

Solution

You are missing a set of enclosing parenthesis for the whole expression, between the start of line and end of line anchors:

 ^(([0-2]?[0-9]:[0-5]?[0-9])|([0-2]?[0-9]:[0-5]?[0-9]:[0-5]?[0-9]))$

Without the enclosing parenthesis, your regex was essentially saying:

  • match ^([0-2]?[0-9]:[0-5]?[0-9])
  • or ([0-2]?[0-9]:[0-5]?[0-9]:[0-5]?[0-9])$

So, 00:00:99 would be valid, matching against the first part of the regex. And, so would something like: 99:00:00:00, which would match against the second part.

That said, your regex is still going to match some unwanted patterns, such as: 29:00

An improved version would be:

^((([0-1]?[0-9])|(2[0-3]):[0-5]?[0-9])|(([0-1]?[0-9])|(2[0-3]):[0-5]?[0-9]:[0-5]?[0-9]))$

OTHER TIPS

Although it does not answer the question directly, I would like to say that in such a standard cases, I would rather use built-in function rather than creating real scary regular expressions:

DateTime.TryParseExact(input, "H:m:s", CultureInfo.InvariantCulture, DateTimeStyles.None, out d);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top