Distributed Networks Distributed Networks

Unix Concepts   «Prev  Next»
Lesson 4 Matching the occurrences of a pattern
Objective Create regular expressions using the ., *, and [ ] metacharacters.
Regular expression syntax provides several metacharacters for matching the occurrence of patterns. The following table shows the most common types:
. Match a single character
* Match zero or more occurrences of the character preceding the *
[ ] Match any character within the brackets, or match a character within a range, such as [0-9], [A-Z], or [a-z]

Note the similarity between these metacharacters and the shell’s file name wildcards. The shell uses a question mark (?) to match a single character, whereas regular expressions use a dot (.). In file name matching, the shell uses * to match one or more of the preceding characters. Using * with regular expressions matches zero or more characters. Brackets work the same way in either situation.
You can combine metacharacters. For example, you can use .* to match zero or more instances of any character.
The following slide show shows some grep commands that search the file /usr/dict/words. This system file is a standard part of many versions of UNIX. It contains a list of words, one per line.
Special Characters Regular Expressions
In the next lesson, you will learn how to use the caret (^) and dollar sign ($) metacharacters to match the position of a pattern.
Matching Patterns - Quiz
Click the Quiz link below to answer some questions about regular expressions, quoting, and the
., *, and [ ]
Matching Patterns - Quiz