CodexBloom - Programming Q&A Platform

Regex Failing to Match ISO 8601 Date Formats in Python - Need Help with Timezone Handling

👀 Views: 2 đŸ’Ŧ Answers: 1 📅 Created: 2025-06-09
regex python datetime Python

I've looked through the documentation and I'm still confused about I'm trying to validate and extract dates in ISO 8601 format (e.g., `2023-10-15T14:27:00Z` and `2023-10-15T14:27:00+02:00`) using Python's `re` module. I wrote the following regex pattern to capture the date and time correctly: ```python import re iso_date_pattern = r'^(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:Z|[+-]\d{2}:\d{2})?)$' test_dates = [ '2023-10-15T14:27:00Z', '2023-10-15T14:27:00+02:00', '2023-10-15T14:27:00-05:00', '2023-10-15 14:27:00', # Should fail '10-15-2023T14:27:00Z' # Should fail ] for date in test_dates: if re.match(iso_date_pattern, date): print(f'{date} is a valid ISO 8601 date.') else: print(f'{date} is NOT a valid ISO 8601 date.') ``` However, when I run this, I get the following output: ``` 2023-10-15T14:27:00Z is a valid ISO 8601 date. 2023-10-15T14:27:00+02:00 is a valid ISO 8601 date. 2023-10-15T14:27:00-05:00 is a valid ISO 8601 date. 2023-10-15 14:27:00 is NOT a valid ISO 8601 date. 10-15-2023T14:27:00Z is NOT a valid ISO 8601 date. ``` While the valid cases are correctly identified, I noticed that the regex does not allow optional fractional seconds, which is part of the ISO 8601 standard (e.g., `2023-10-15T14:27:00.123456Z`). How can I modify the regex to include optional milliseconds or microseconds? Also, how can I ensure that the timezone offset is properly validated, especially regarding the minutes? I want to support any valid ISO 8601 date format without overly complicating the regex. I'm using Python 3.9 and the `re` module for this. Any suggestions to enhance this pattern would be greatly appreciated! What's the best practice here? I'm working on a API that needs to handle this. Thanks in advance!