? REPEAT PREVIOUS EXPRESSION ZERO OR ONE TIME

? REPEAT PREVIOUS EXPRESSION ZERO OR ONE TIME

===> egrep ' (C-)?[sS]hells ' fortunes

He sells C-Shells by the C-Shore.

She sells sea shells by the sea shore.

===> egrep ' p[aeiou]?p.. ' fortunes

Line printer paper is strongest at preforations.

Previous slide Next slide Back to the first slide View Graphic Version

Notes:

The first example matches a space; followed by the characters C- (which can occur zero or one time); followed by the character string Shell or shell; then completing the expression with a space. The result: lines 14 and 26 satisfies this match with the strings C-Shells and shells respectively from the fortunes file. The matches are underlined in this example.

In the second example, the expression wants to match a space; followed by a p; then a vowel that can occur zero or one time; after the possible vowel match, match another p; followed by any two printable characters; then close the expression out matching a space. Line 19 satisfies this match with the word paper from the fortunes file.