Natural Language Processing, Lab assignment 2.

Due Feb 3, 6:15PM.

Working with Regular Expressions in Python

You can use Python on your own computer or in this lab.

Start the interactive Python interpreter and enter the following at the prompt:

 import re
 pat = re.compile('baaa*!!')
 while 1:
	line = raw_input('Are you a sheep? ("q to quit):')
	if line == 'q':
	if pat.match(line):
		print 'Yep. You said " ' +line +'"'
		print 'Nope. You said " ' +line +'"'

You have defined a pattern using a regular expression and used it to match the input.

  1. Experiment with this code interactively to see what inputs it "accepts"
  2. Change from using match to using search
  3. Begin Working through the Python 201 tutorial on regular expressions by Dave Kuhlman at: While doing so, figure out how to extract the matched text and use that in the "accept" print statement (instead of line).
  4. Capture the output of your session and turn it in at the beginning of the next class.


Once you've carried out the above, it is advisable to switch to a gentler introduction to regular expressions:


Do Exercises 2.1, 2.3 (also draw the FSA) using Python. Exercise 2.6 on paper only.