Advertisement
horozov86

dates_and_times

Sep 14th, 2024
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.24 KB | None | 0 0
  1. import re
  2.  
  3.  
  4. def extract_dates_and_times(text):
  5.  
  6.     date_patterns = [
  7.         r'\b\d{4}-\d{2}-\d{2}\b',
  8.         r'\b\d{4}/\d{2}/\d{2}\b',
  9.         r'\b(?:January|February|March|April|May|June|July|August|September|October|November|December) \d{1,2}(?:th|st|nd|rd)?\b',
  10.     ]
  11.  
  12.     time_patterns = [
  13.         r'\b\d{1,2}:\d{2}(?:am|pm)?\b',
  14.         r'\b\d{1,2}:\d{2}\b'
  15.     ]
  16.  
  17.     date_regex = re.compile('|'.join(date_patterns), re.IGNORECASE)
  18.     time_regex = re.compile('|'.join(time_patterns), re.IGNORECASE)
  19.  
  20.     dates = date_regex.findall(text)
  21.     times = time_regex.findall(text)
  22.  
  23.     dates = list(set(dates))
  24.     times = list(set(times))
  25.  
  26.     return dates, times
  27.  
  28.  
  29. text = """Hi,
  30. my name is Jane and my phone number is 555-123-4567.
  31. My email address is jane_doe@example.com.
  32. I live on 123 Main St. Apt. #456, and I was born on January 11th, 1990. I have an appointment on 2023-05-15 at 2:30pm at 789 Oak Ln. #3 and backup on 2023/05/21.
  33. Please give me a call or send me an email to confirm. In case the dates are unavailable, please set up a meeting sometime in June. I would love June 19th around 14:00.
  34. Thank you!"""
  35.  
  36. dates, times = extract_dates_and_times(text)
  37.  
  38. print("Found dates:", dates)
  39. print("Found times:", times)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement