# reexample.py
#
# Examples using the re module

# Some text
text = "Guido will be out of the office from 12/14/2012 to 1/3/2013."

# A Pattern for dates such as 12/3/2009

import re
datepat = re.compile(r'(\d+)/(\d+)/(\d+)')

# Search text and print out all dates
for m in datepat.finditer(text):
    print m.group()

# Print out dates in a different format
monthnames = ['January','February','March','April','May','June',
              'July','August','September','October','November','December']

for m in datepat.finditer(text):
    month = m.group(1)
    day   = m.group(2)
    year  = m.group(3)
    print "%s %s, %s" % (monthnames[int(month)-1],day,year)

# Replace dates in text with a different format

def change_date(m):
    month = m.group(1)
    day   = m.group(2)
    year  = m.group(3)

    return "%s %s %s" % (day, monthnames[int(month)-1],year)

newtext = datepat.sub(change_date,text)
print newtext
