Last active
August 29, 2015 14:11
-
-
Save imjaypatel/182f42b4ed38046682ed to your computer and use it in GitHub Desktop.
Calender Agenda Regexp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
def time_replace(timeword): | |
timestr = timeword.group().lower() | |
flag = None | |
if 'h' in timestr: | |
flag = 1 | |
elif ':' in timestr: | |
flag = 2 | |
elif 'm' in timestr: | |
flag = 3 | |
elif 'at' in timestr: | |
return "Location" | |
if flag == 1: | |
hour = timestr.split('h')[0] | |
if int(hour) > 12 and int(hour) < 24: | |
return str(int(hour) - 12) + ":00 PM" | |
elif int(hour) == 24: | |
return str(int(hour) - 12) + ":00 AM" | |
elif int(hour) == 12: | |
return hour + ":00 PM" | |
else: | |
return hour + ":00 AM" | |
elif flag == 2: | |
hour, minute = timestr.split(':') | |
if int(hour) > 12 and int(hour) < 24: | |
return str(int(hour) - 12) + ":" + minute + " PM" | |
elif int(hour) == 24: | |
return str(int(hour) - 12) + ":" + minute + " AM" | |
elif int(hour) == 12: | |
return hour + ":" + minute + " PM" | |
else: | |
return hour + ":" + minute + " AM" | |
elif flag == 3: | |
time = re.search(r"\d+(\.\d+)?", timestr).group() | |
hour, ampm = time[-2:], timestr[-2:] | |
meridiem = " PM" if 'pm' in ampm else " AM" | |
return "%s:00%s" % (hour, meridiem) | |
else: | |
return timestr | |
#agenda = raw_input("Enter Agenda Message: ") | |
agenda = """ | |
--------------------------------------- | |
8h going swimming with friends | |
16H going swimming with friends | |
8:00 going swimming with friends | |
08:35 going swimming with friends | |
8pm going for dinner | |
8AM going swimming with friends | |
7PM going for dinner at ahmedabad | |
9am going swimming with friends""" | |
print agenda | |
print re.sub(r"(\d{1,2}h)|(\d{1,2}:\d{2})|(\d{1,2}(?:am|pm))|((?!\s)at(?=\s))", time_replace, agenda, flags=re.I|re.M) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment