# ACT 2-7

# The import statement below tells Python to load a
# set of 'regular expression' functions.  If you use
# regular expressions in your programs, you NEED
# to have this line.
import re

def nameGame(name):
    '''Given a name, returns a rhyme.'''
    name = name.lower()
    firstLetter = name[0]
    if isVowel(firstLetter):
        suffix = name
    else:
        suffix = name[1:len(name)]
        
    myStr = name + ' ' + name + ' bo-b' + suffix + '\n'
    myStr = myStr + 'banana-fana fo-f' + suffix + '\n'
    myStr = myStr + 'me my mo m' + suffix + '\n'
    myStr = myStr + name + '!'
    return myStr

def isVowel(character):
    '''Returns True if character is a lowercase vowel and False otherwise.'''
    if ((character == 'a') or
        (character == 'e') or
        (character == 'i') or
        (character == 'o') or
        (character == 'u')):
        return True
    else:
        return False

def isVowelRegEx(character):
    '''Returns True if character is a lowercase vowel and False otherwise'''
    # re.search(regex,string) returns None if no match is found
    if re.search('[aeiou]',character) == None:
        return False
    return True

def readShel():
    '''Reads the Shel poem and returns a string.'''
    fileName = 'poem.txt'
    myFile = open(fileName,'r')
    myString = myFile.read()
    myFile.close()
    return myString

def printRegex(regex,myStr):
    '''Prints all occurrences of the regular expression.'''
    myIter = re.finditer(regex,myStr) # NEW object! It's called an iterator.

    # This iterator contains MATCHES of the regex.
    # The following functions work on regex matches:
    # (the '0' means something, but we won't talk about it today)
    #  group(0): returns the string that matches the regex
    #  start(0): the starting position of the string in myStr
    #  end(0): the ending position of the string in myStr

    # For loops work on iterators in addition to lists.
    # Each match of the regex in myStr is stored in
    # a variable called 'occurrence'.
    for occurrence in myIter:
        # The '\' allows me to continue on the next line.
        print 'matches',occurrence.group(0), \
              'at positions',occurrence.start(0),'-',occurrence.end(0)
    return


