UTC Offset Regex for Python
/^(?:UTC)?([+-])(2[0-3]|[01][0-9]):([0-5][0-9])$|^(?:UTC)?([+-])(14):(00)$|^(?:UTC|Z|0)$/What this pattern does
This page provides a comprehensive, battle-tested regular expression for matching utc offset, ported and verified for Python. A rigorously tested regex reduces debugging time and protects your application from edge-case failures. The snippet below is ready to drop into your Python project — whether you're validating in a Django view, a FastAPI endpoint, or a standalone data processing script.
Python Implementation
# UTC Offset
# ReDoS-safe | RegexVault — Localization > Time Formats
import re
utc_offset_pattern = re.compile(r'^(?:UTC)?([+-])(2[0-3]|[01][0-9]):([0-5][0-9])$|^(?:UTC)?([+-])(14):(00)$|^(?:UTC|Z|0)$')
def validate_utc_offset(value: str) -> bool:
return bool(utc_offset_pattern.fullmatch(value))
# Example
print(validate_utc_offset("UTC")) # TrueTest Cases
Matches (Valid) | Rejects (Invalid) |
|---|---|
UTC | +25:00 |
Z | +05:75 |
+00:00 | 05:30 |
+05:30 | UTC8 |
-05:00 | +5:30 |
+09:30 | +05:3 |
-03:30 | — |
+14:00 | — |
UTC+08:00 | — |
When to use this pattern
This pattern is drawn from the Localization > Time Formats category and carries a ReDoS-safe certification. That matters for Python developers because particularly important in Python web servers where CPU-bound regex operations can stall concurrent request handling. RegexVault audits patterns against known backtracking attack vectors, ensuring you have the necessary context before using this regex in a high-stakes production environment.
Common Pitfalls
+14:00 (Kiribati/Line Islands) is the most positive real-world offset. Any offset beyond ±14:00 is not a valid IANA timezone — reject it.
Technical Notes
Real-world range: -12:00 (US Minor Outlying Islands) to +14:00 (Line Islands, Kiribati). Half-hour and 45-minute offsets exist. Nepal (+05:45) is the only 45-minute zone in common use.
Have a pattern that belongs in the vault?
Submit it for review — community-verified patterns get credited to your GitHub handle. Free submissions join the queue. Priority review available for $15.
Submit a Pattern