Activity 3-2

March 19, 2013

Task 1: String Matching & Regular Expressions

The website http://regexpal.com/ allows you to demo regular expressions on sample text. We will use the page in the steps below. Enter regular expressions in the top box and sample text in the bottom box.

  1. Use the function nameGame() in ACT3-2.py to make a rhyme with your name. Copy it into the bottom box.
  2. Enter the following lines into the top box. What do you think brackets do?
    • n
    • fn
    • [fn]
    • [aeiou]
  3. Get all the words that that prepend the letters b, f, or m to your name (or the suffix of your name).
  4. Enter the following lines into the top box. What do you think \w does? What do you think the + sign does?
    • f
    • f\w
    • f\w\w
    • f\w+
  5. Enter the following lines into the top box. What do you think \s does?
    • \s
    • \sm
  6. Get all the words that start with b, f, or m.
  7. Remember that \n is a special character that denotes a line break. Get all the words that appear at the end of a line.
  8. Add some numbers to the bottom box. Test what \d does.

Task 2: More Examples of Regular Expressions

Go back to ACT3-2.py. Download and save poem.txt in the same directory as ACT3-2.py (remember why we need to put poem.txt in the same directory as the python program?). Press F5.

  1. Run the following statements and figure out what each line does.
    myStr = readShel()
    
    printRegex('g\w+', myStr)
    printRegex('\sg\w+', myStr)
    printRegex('\s[gG]\w+', myStr)
    
    printRegex('out', myStr)
    printRegex('\sout', myStr)
    printRegex('\wout', myStr)
    printRegex('\w+out', myStr)
    printRegex('[\s\w]out', myStr)
    printRegex('[\s\w][Oo]ut', myStr)
    
  2. Design regular expressions that print the following. Don't worry about capitalization (just get all instances of lowercase).
    1. Print all occurrences of the substring it.
    2. Print all occurrences of the word it. A word should be surrounded by whitespace. There are five instances of the word it.
    3. Print all words that contain it and at least one other letter. There are six such words.
    4. Print all words that end in ing. There are two such words.
    5. Print all phrases surrounded by double quotes (all occurrences of speech). There is only one phrase.
    6. Print all contractions (words with single quotes). Remember that single quotes are “special” - they require a \. There are two such words (She'd and I'll), but write the expression to return any contraction.