Regex for Parsing Custom Time Ranges in Python - implementing Overlapping Intervals
I'm not sure how to approach I'm experimenting with I've hit a wall trying to I'm relatively new to this, so bear with me... I'm trying to use regex in Python to parse time ranges from a string input formatted like '10:00-12:00, 13:00-15:30'. The goal is to extract the start and end times while ensuring that the intervals do not overlap. The regex I wrote is as follows: ```python import re time_ranges = '10:00-12:00, 13:00-15:30' pattern = r'(\d{2}:\d{2})-(\d{2}:\d{2})' matches = re.findall(pattern, time_ranges) print(matches) ``` This outputs: ``` [('10:00', '12:00'), ('13:00', '15:30')] ``` However, I need to seem to figure out how to check for overlapping intervals after parsing them. For instance, if I input '10:00-12:00, 11:30-13:00', it should flag this as an behavior. I considered adding some logic after the regex match to check for overlaps, but I'm unsure how to implement it cleanly. Here's what I attempted next: ```python def check_overlaps(ranges): for i in range(len(ranges)): start1, end1 = map(lambda x: datetime.strptime(x, '%H:%M'), ranges[i]) for j in range(i + 1, len(ranges)): start2, end2 = map(lambda x: datetime.strptime(x, '%H:%M'), ranges[j]) if start1 < end2 and start2 < end1: print(f'Overlap detected between {ranges[i]} and {ranges[j]}') ``` But when I run my full program, I get this behavior: ``` TypeError: want to unpack non-iterable str object ``` I suspect this is due to the way I'm trying to extract the times from `matches`. Could anyone suggest a way to properly handle this, or if there's a more efficient method to validate the time ranges against overlaps? I'm using Python 3.9.1 and the `datetime` module for time comparisons here. Any help would be greatly appreciated! For context: I'm using Python on Windows. Any ideas what could be causing this? This is part of a larger application I'm building. Thanks for taking the time to read this! Hoping someone can shed some light on this.