Advertisement

Pattern library / Dates & Time

Regex to Match Time in HH:MM (or HH:MM:SS) Format

A clock time is four digits and a colon — until 99:99 matches too. This pattern limits hours to 00–23 and minutes/seconds to 00–59, with seconds optional, in one line that works identically in JavaScript, Python, SQL and RE2 systems.

The pattern
^([01][0-9]|2[0-3]):[0-5][0-9](:[0-5][0-9])?$
Load this pattern into the tester →

How the pattern works

Test it: match & no-match examples

Every row is loaded straight into the tester — click Try to see the match live:

InputResultWhy
14:30Matchvalid 24h timeTry →
00:00MatchmidnightTry →
23:59:59Matchwith secondsTry →
24:00No matchhour max 23Try →
14:60No matchminute max 59Try →
2:30 PMNo match12-hour AM/PM not coveredTry →

Where to use it

JavaScript

const re = /^([01][0-9]|2[0-3]):[0-5][0-9](:[0-5][0-9])?$/;
re.test('23:59:59'); // true

Python

import re
bool(re.fullmatch(r'([01][0-9]|2[0-3]):[0-5][0-9](:[0-5][0-9])?', '14:30'))

SQL (Postgres / BigQuery)

SELECT REGEXP_MATCHES('14:30', '^([01][0-9]|2[0-3]):[0-5][0-9](:[0-5][0-9])?$');

Common mistakes

Test this pattern on your own data
Paste it into the free tester with your real strings — nothing leaves your browser.
Open the regex tester →

FAQ

How do I match both HH:MM and HH:MM:SS with one regex?

Make the seconds group optional: ^([01][0-9]|2[0-3]):[0-5][0-9](:[0-5][0-9])?$. One pattern matches 14:30 and 14:30:00.

Why does 24:00 not match?

The hour branch is ([01][0-9]|2[0-3]), so the maximum is 23. ISO 8601 allows 24:00 as an edge case for end-of-day; add it as an alternative — 24:00 — if your data uses it.

Can I use this in GA4?

Yes, it is plain RE2 syntax. Wrap it for partial matching (.* around it) if your GA4 field contains more than the time itself.

ISO 8601 DateTime Regex — Match & Test Online · Regex to Match Date (YYYY-MM-DD) — Test & Copy · Log Level Regex — Match INFO / WARN / ERROR Lines

For GA4-specific patterns, see the GA4 regex cheat sheet, the page path regex guide, the event filter regex guide, the GA4 regex match types, the custom channel grouping guide and RE2 vs PCRE. Browse the full regex pattern library.