OnWorks Linux and Windows Online WorkStations

Logo

Free Hosting Online for WorkStations

< Previous | Contents | Next >

Alternation

The first of the extended regular expression features we will discuss is called alternation, which is the facility that allows a match to occur from among a set of expressions. Just as a bracket expression allows a single character to match from a set of specified characters, alternation allows matches from a set of strings or other regular expressions.

To demonstrate, we’ll use grep in conjunction with echo. First, let’s try a plain old string match:



[me@linuxbox ~]$ echo "AAA" | grep AAA

AAA

[me@linuxbox ~]$ echo "BBB" | grep AAA

[me@linuxbox ~]$

[me@linuxbox ~]$ echo "AAA" | grep AAA

AAA

[me@linuxbox ~]$ echo "BBB" | grep AAA

[me@linuxbox ~]$


A pretty straightforward example, in which we pipe the output of echo into grep and see the results. When a match occurs, we see it printed out; when no match occurs, we see no results.

Now we’ll add alternation, signified by the vertical-bar metacharacter:



[me@linuxbox ~]$ echo "AAA" | grep -E 'AAA|BBB'

AAA

[me@linuxbox ~]$ echo "BBB" | grep -E 'AAA|BBB'

BBB

[me@linuxbox ~]$ echo "CCC" | grep -E 'AAA|BBB'

[me@linuxbox ~]$

[me@linuxbox ~]$ echo "AAA" | grep -E 'AAA|BBB'

AAA

[me@linuxbox ~]$ echo "BBB" | grep -E 'AAA|BBB'

BBB

[me@linuxbox ~]$ echo "CCC" | grep -E 'AAA|BBB'

[me@linuxbox ~]$


Here we see the regular expression 'AAA|BBB', which means “match either the string AAA or the string BBB.” Notice that since this is an extended feature, we added the -E option to grep (though we could have just used the egrep program instead), and we enclosed the regular expression in quotes to prevent the shell from interpreting the verti- cal-bar metacharacter as a pipe operator. Alternation is not limited to two choices:



[me@linuxbox ~]$ echo "AAA" | grep -E 'AAA|BBB|CCC'

AAA

[me@linuxbox ~]$ echo "AAA" | grep -E 'AAA|BBB|CCC'

AAA


To combine alternation with other regular expression elements, we can use () to separate the alternation:



[me@linuxbox ~]$ grep -Eh '^(bz|gz|zip)' dirlist*.txt

[me@linuxbox ~]$ grep -Eh '^(bz|gz|zip)' dirlist*.txt


This expression will match the filenames in our lists that start with either “bz”, “gz”, or “zip”. Had we left off the parentheses, the meaning of this regular expression :



[me@linuxbox ~]$ grep -Eh '^bz|gz|zip' dirlist*.txt

[me@linuxbox ~]$ grep -Eh '^bz|gz|zip' dirlist*.txt


changes to match any filename that begins with “bz” or contains “gz” or contains “zip”.


Top OS Cloud Computing at OnWorks: