[Edu-sig] Regular Expressions

Brent Burley and Pat Erb erburley@ix.netcom.com
Tue, 29 May 2001 22:08:59 -0700


Does anyone teach regular expressions?  I think it's one of the most
powerful programming tools (and from my informal observations, one of the
most overlooked).  A rule of thumb I use in Python (and Perl) is that if
you're accessing individual characters in a string, especially in a loop,
there's a good chance it would be better done with a regular expression.

Regular expressions can be intimidating to the uninitiated, and even the
experienced often require several tries to get an expression right.
Python's interactive nature helps, but it's fairly tedious to test a regular
expression at a python console.

I've written a tool called Recon , the Regular Expression Test Console.  I
use it for debugging regular expressions, but I also think it would be a
great teaching tool since it provides a very simple, focused environment
with detailed feedback.  It's best described by the screen shot:
http://erburley.home.netcom.com/recon.html

If you want some exercises, try to write expressions for the following:
- date (single or multiple formats)
- currency
- numbers, int/float/fraction/hex/e-notation/etc
- words (consider line breaks and hyphens for a challenge!)
- doubled words (as in "the the")
- phone number, w/ international prefix
- program identifiers
- python comments (don't forget to skip # in a string)
- C++ comments (hard to do right!)
- python argument lists, including keyword args

        Brent Burley