shorten the weekdays from texts
Question: 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.


Answer: seems like a simple
[`str.replace`](http://docs.python.org/2/library/stdtypes.html#str.replace)
should work here:

    
    
    new_paragraph = paragraph.replace('Monday','Mon')
    

You can even use the
[`calendar`](http://docs.python.org/2/library/calendar.html#calendar.day_name)
module to make this a little easier:

    
    
    import calendar
    
    for day,abbr in zip(calendar.day_name,calendar.day_abbr):
        paragraph = paragraph.replace(day,abbr)
    

* * *

for _really_ big paragraphs, it might be worthwhile to switch over to regex:

    
    
    import re
    import calendar
    
    regex = re.compile('|'.join(calendar.day_name))
    sub_dict = dict(zip(calendar.day_name,calendar.day_abbr))
    new_paragraph = regex.sub(lambda match: sub_dict[match.group(0)],paragraph)
    

Of course, with calendar, the abbreviations/etc are locale dependent (but
maybe that's desirable anyway)

