CS362 2011F Compilers

Laboratory: Regular Expressions

Summary: In this lab, you will explore regular expressions in the Unix environment.

Collaboration: Feel free to work on this lab in pairs or trios.

Background: In this lab, you'll use regular expressions to look for English words that match particular patterns. You can find a list of English words in /usr/share/dict/words.

The standard command for searching using regular expressions is grep (and no, it is not "GNU rep"). grep stands for something like general regular expression patterns. You traditionally use grep in a form like the following:

% grep 'regexp' file

It is important to put the regular expression in single quotes so that the shell does not interpret special characters, like braces, parens, and stars.

Here are the basic mechanisms for building a grep-style regular expression. You should be able to find more in the man page.

What can you do with the output from grep? You can put the results in a new file.

% grep 'regexp' file  > results-file

You can look at the results by piping them through less or more.

% grep 'regexp' file  | less

You can simply count the results by piping them through wc, the word-count program.

% grep 'regexp' file  | wc -l

You can even send the results through another invocation of grep (which is a nice way to get values accepted by both regular expressions; that is, the interesection of two langauges).

% grep 'regexp1' file | grep 'regexp2' | ...

Questions: Write a regular expression for each of the following and determine how many words in the Unix dictionary match the regular expression.

  1. Words starting with a
  2. Words starting with A
  3. Words that start with a or A
  4. Words with exactly four letters
  5. Words with exactly four letters and begins with a
  6. Words that contain a capital letter
  7. Words that start with non-capital and include a capital
  8. Words with more than one capital
  9. Words that neither begin nor end with a
  10. Words that begin and end with a vowel
  11. Words that neither begin nor end with a vowel
  12. Words that match your phone number, using the normal number-letter conversion: 2 is abc, 3 is def, 4 is ghi, and so on and so forth (if your phone number includes a 0 or a 1, try the digits on either side of the 0 or 1)
  13. Words that contain the vowels in order (and, perhaps, intervening letters)
  14. Words that contain your initials in order (and, perhaps, intervening letters)
  15. Words that begin with sc or xy
  16. Words with four or more letters
  17. Words that contain only the letters of your first name



Thursday, 1 February 2001 [Samuel A. Rebelsky]

  • Created as an ad hoc lab (narrated the content to the class).

Monday, 9 September 2002 [Samuel A. Rebelsky]

  • Created a draft of this document from the notes of Andrew Kensler.

Tuesday, 10 September 2002 [Samuel A. Rebelsky]

  • First released version of the lab.

Wednesday, 4 February 2004 [Samuel A. Rebelsky]

Friday, 26 August 2011 [Samuel A. Rebelsky]


Disclaimer: I usually create these pages on the fly, which means that I rarely proofread them and they may contain bad grammar and incorrect details. It also means that I tend to update them regularly (see the history for more details). Feel free to contact me with any suggestions for changes.

This document was generated by Siteweaver on Fri Sep 9 12:23:28 2011.
The source to the document was last modified on Fri Sep 9 12:23:26 2011.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS362/2011F/Labs/regexp.html.

Samuel A. Rebelsky, rebelsky@grinnell.edu