() Grouping of Regular Expressions

() Grouping of Regular Expressions

==> egrep ' with(out) ' fortunes

A woman without a man is like a fish without a bicycle.

==> egrep ' with(out)? ' fortunes

A truly wise man never plays leapfrog with a Unicorn.

A woman without a man is like a fish without a bicycle.

An elephant is a mouse with an operating system.

Writing free verse is like playing tennis with the net down.

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

Notes:

We have illustrated how UNIX regular expressions provide tremendous flexibility; with regards to how special characters are used to affect a portion (subexpression) of the complete expression. We can expand this further: when mastering the grouping of subexpressions within parentheses; then using a special spanning of characters operation..

In the first example, we’re not doing anything special other then looking for the word without. Notice there is no affect yet by grouping the substring out within parentheses. As a result, line 4 satisfies the match from the fortunes file.

The second illustrates what happens when we have the question mark (interpreted as have the expression work zero or more times) act on the subexpression out. Notice how we can now match the words with or without, by using a question mark (?) to span the grouping of the subexpression out. Based upon the expressions definition, lines 3, 4, 5, and 13 satisfies the pattern match.