Computer Sciences Department logo

CS 368 (Summer 2009) — Day 7 Homework

Due Friday, July 24th, at the start of class.


Write a Perl script and regular expression that matches street addresses. Because it'd be very difficult, if not impossible, to match every possible format of street address, we'll restrict the types of addresses that the parser needs to recognize.


This program should read the list of possible addresses from a file; the name should be read from the command line; or, if you prefer, the the input can be read from the standard input stream. Each line in the file will be a potential address, which your program should verify matches the format described below. The program should print matching address lines to the standard output stream, and ignore any that don't match.

Address Format Details

The general format is:

Example Valid addresses

Example Invalid addresses


You'll need to use "grouping" to do the zip + 4 part working, which we'll discuss in class on Friday. For now, I'll just give you a "magic incantation" to add to the end of your regex. This will either match the entire '-nnnn' portion of the zip, or none of it at all:

Zathras understand. No Zathras not understand, but Zathras do. Zathras good at doings, not understandings.

The regular expression used to match this all is quite long. Build it up in pieces, testing one chunk at a time. Don't forget that the -ne parameters to perl can be your friend:

perl -ne 'if(/expr/) { print; }'


Do the work yourself, consulting reasonable reference materials as needed; any reference material that gives you a complete or nearly complete solution to this problem or a similar one is not OK to use. Asking the instructors for help is OK, asking other students for help is not.


Hand In

A printout of your script on a single sheet of paper. At the top of the printout, please include “CS 368 Summer 2009”, your name, and “Homework 04, July 20, 2009”. Identifying your work is important, or you may not receive appropriate credit.