Compilers (CS362 2002F)

Regular Expressions

Summary: In today's lab, you will explore regular expressions in the Unix environment.

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

Turning It In: Save your answers in a plain text file and submit it using the ECA.

Grading: I expect that you will gain more from doing this lab than from me grading this lab. I will simply scan through your answers to see if you had any particularly valuable insights.

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
  13. Words that contain the vowels in order
  14. Words that contain your initials in order
  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

 

History

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.

 

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 Wed Feb 4 20:15:21 2004.
The source to the document was last modified on Wed Feb 4 20:15:19 2004.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS362/2002F/Labs/lab.02.html.

You may wish to validate this document's HTML ; Valid CSS! ; Check with Bobby

Glimmer Labs: The Grinnell Laboratory for Interactive Multimedia Experimentation & Research
glimmer@grinnell.edu