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]\d | hours 00 through 19 |
| 2[0-3] | hours 20 through 23 |
| [0-5]\d | minutes 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.
| Input | Result | Matched |
|---|---|---|
| 00:00 | matches | 00:00 |
| 09:30 | matches | 09:30 |
| 23:59 | matches | 23:59 |
| 24:00 | no match | — |
| 9:30 | no match | — |
| 23:60 | no match | — |
| 23:59:59 | no 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 RegexBuilderOther regex answers
- Regex for an email address
- Regex for a US phone number
- Regex for an IPv4 address
- Regex for a URL
- Regex for a date in YYYY-MM-DD format
- Regex to match digits only
- Regex for alphanumeric characters
- Regex to find special characters
- Regex for a strong password
- Regex to match whitespace
- Regex to trim leading and trailing whitespace
- Regex for a UUID