Reputation: 2166
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.
Upvotes: 0
Views: 111
Reputation: 55720
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:
^([0-2]?[0-9]:[0-5]?[0-9])
([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]))$
Upvotes: 2
Reputation: 16878
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);
Upvotes: 0