[Skip to Body]
Primary:
[Front Door]
[Schedule]
[Piazza]
-
[Academic Honesty]
[Instructions]
Current:
[Current Outline]
[Current EBoard]
-
[Current Assignment]
[Current Lab]
Groupings:
[Assignments]
[Documents]
[EBoards]
[Examples]
[Exams]
[Handouts]
[Labs]
[Outlines]
[Readings]
Related Courses:
[CSC362 2004S (Rebelsky)]
[CSC362 2010S (Stone)]
Misc:
[SamR]
[GNU Coding Standards]
[Dragon Book]
[Pascal Standards]
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.
ab
: concatenation without parens
[abc]
: alternation of single characters
[^aeiou]
: all but vowels
*
: zero or more copies
.
: any character
^
: start of line
$
: end of line
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.
Thursday, 1 February 2001 [Samuel A. Rebelsky]
ad hoclab (narrated the content to the class).
Monday, 9 September 2002 [Samuel A. Rebelsky]
Tuesday, 10 September 2002 [Samuel A. Rebelsky]
Wednesday, 4 February 2004 [Samuel A. Rebelsky]
http://www.cs.grinnell.edu/~rebelsky/Courses/CSC362/2004S/Labs/lab.03.html
.
Friday, 26 August 2011 [Samuel A. Rebelsky]
http://www.cs.grinnell.edu/~rebelsky/Courses/CSC362/2011F/Labs/regexp.html
.
[Skip to Body]
Primary:
[Front Door]
[Schedule]
[Piazza]
-
[Academic Honesty]
[Instructions]
Current:
[Current Outline]
[Current EBoard]
-
[Current Assignment]
[Current Lab]
Groupings:
[Assignments]
[Documents]
[EBoards]
[Examples]
[Exams]
[Handouts]
[Labs]
[Outlines]
[Readings]
Related Courses:
[CSC362 2004S (Rebelsky)]
[CSC362 2010S (Stone)]
Misc:
[SamR]
[GNU Coding Standards]
[Dragon Book]
[Pascal Standards]
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
.