Free Hosting Online for WorkStations

< Previous | Contents | Next >

Bracket Expressions And Character Classes

In addition to matching any character at a given position in our regular expression, we can also match a single character from a specified set of characters by using bracket ex- pressions. With bracket expressions, we can specify a set of characters (including charac- ters that would otherwise be interpreted as metacharacters) to be matched. In this exam- ple, using a two character set:



[me@linuxbox ~]$ grep -h '[bg]zip' dirlist*.txt

bzip2 bzip2recover gzip

[me@linuxbox ~]$ grep -h '[bg]zip' dirlist*.txt

bzip2 bzip2recover gzip


we match any line that contains the string “bzip” or “gzip”.

A set may contain any number of characters, and metacharacters lose their special mean- ing when placed within brackets. However, there are two cases in which metacharacters are used within bracket expressions, and have different meanings. The first is the caret (^), which is used to indicate negation; the second is the dash (-), which is used to indi- cate a character range.


 

Top OS Cloud Computing at OnWorks: