I am writing a python library that takes a paragrah that consists of different types of hours format. Paragraph is actually taken from different restaurant sites, they have hours of operation in various formats for each site. So, I decided to write a library that takes any format of (eg: M-Fri 10am – 12pm, M, Tue, Wed 00:00-18:00 etc.). My decision is to write different set of RegEx that takes a string and proceeds if it matches any of my pattern.
I have choosen RegEx pattern matching because, instead of writing own logics for every pattern and replacing just using str.replace may takes a lot of time. But I stuck in regex since I am new to it. So any of you can suggest me how can I do the following task?
As a first step I would like to shorten all the weekday formats to 3 letter shorten week days as below.
Example:
“Monday 09:00 AM – 5:00 PM, Tuesday 09:00 AM – 5:00 PM, Wednesday 09:00 AM – 5:00 PM, Thursday 09:00 AM – 5:00 PM, Friday 09:00 AM – 5:00 PM, Saturday Closed, Sunday Closed“
Output should be
“Mon 09:00 AM – 5:00 PM, Tue 09:00 AM – 5:00 PM, Wed 09:00 AM – 5:00 PM, Thu 09:00 AM – 5:00 PM, Fri 09:00 AM – 5:00 PM, Sat Closed, Sun Closed”
Thanks.
seems like a simple
str.replaceshould work here:You can even use the
calendarmodule to make this a little easier:for really big paragraphs, it might be worthwhile to switch over to regex:
Of course, with calendar, the abbreviations/etc are locale dependent (but maybe that’s desirable anyway)