RRegexBuilder
Get RegexBuilder

RegexBuilder · regex answers

Regex for a 24-hour time

/^([01]\d|2[0-3]):[0-5]\d$/

Hours split into two alternatives because 00-19 and 20-23 have different second digits. Minutes are simpler: the first digit never exceeds 5. Add (:[0-5]\d)? if seconds are optional in your input.

What each part does

[01]\dhours 00 through 19
2[0-3]hours 20 through 23
[0-5]\dminutes 00 through 59

Tested against real input

Every row below was run through the engine when this page was built. The third column is the substring the pattern actually matched.

InputResultMatched
00:00matches00:00
09:30matches09:30
23:59matches23:59
24:00no match
9:30no match
23:60no match
23:59:59no match

The mistake to avoid

Allowing 24:00. It appears in some standards as an end-of-day marker but almost no library will parse it, so accept it only if you have decided to handle it.

Try it on your own text

Paste this pattern and your input, see each match highlighted with its capture groups, step through what the engine did, and get the equivalent syntax for JavaScript, Python, Java, Go and PCRE.

Open RegexBuilder

Other regex answers