Regular Expressions   «Prev  Next»
Lesson 5 Matching the position of a pattern
Objective Create regular expressions using the ^ and $ metacharacters.

Matching Position of Pattern

Create a regular expression using the ^ and $ metacharacters in Unix
In Unix, regular expressions provide a powerful and flexible way to match patterns in text. The metacharacters ^ and $ are used to define the start and end of a line, respectively. They help in anchoring the pattern to the beginning or the end of the line, ensuring that the match occurs only at the specified position. Here is an example of a regular expression utilizing the ^ and $ metacharacters:
'^[0-9]{4}$'

This regular expression pattern can be broken down as follows:
  1. ^: The caret (^) metacharacter indicates that the pattern must start at the beginning of the line.
  2. [0-9]: This character class denotes a single digit from 0 to 9.
  3. {4}: The curly braces ({}) specify a quantifier, indicating that the preceding pattern (in this case, [0-9]) must occur exactly 4 times.
  4. $: The dollar sign ($) metacharacter signifies that the pattern must end at the end of the line.

Thus, the given regular expression will match a line containing exactly four digits (from 0 to 9) and nothing else. By utilizing the ^ and $ metacharacters, the pattern is anchored to the start and end of the line, ensuring that no additional characters are present before or after the four digits.
In conclusion, the ^ and $ metacharacters in Unix regular expressions are essential for anchoring patterns to the start and end of a line, respectively. They offer precise control over the position of the pattern match within the input text, which is invaluable in various text processing and pattern matching tasks.
By default, regular expressions can match a pattern anywhere within a line, but you can refine this behavior. To match patterns only at the beginning of a line, place a caret (^) before the regular expression. To match patterns only at the end of a line, place a dollar sign ($) after the regular expression.
Here are some examples.

Command Description
grep ^stat file Matches all lines that begin with patterns like static or stately.
grep stat$ file Matches all lines that end with patterns like rheostat.


Anchors

The ^ and $ metacharacters are sometimes called anchors[1]. In the examples above, note that the expressions do not require quotes because they do not contain characters, such as spaces, *, or [ ], that would be interpreted by the shell. By themselves, the ^ and $ do not cause problems for the shell. However, here is a situation where quotes are needed because of the brackets:
% grep "^[Ss]tat" file

This command displays lines that begin with the patterns Stat or stat. But watch what happens using a similar combination with the $metacharacter. The following examples seeks to match ball or bell at the end of a line, but instead the shell displays an error message:
% grep "b[ae]ll$" file

Variable syntax

When the shell sees the $ inside double quotes, the shell assumes it’s working with a shell variable[2].
To make the $ act like an anchor metacharacter, you should use single quotes:
% grep 'b[ae]ll$' file 
When working with regular expressions on the command line, it is best to use single quotes. That way, you do not need to remember whether to use single quotes, double quotes, or no quotes.
In the next lesson, you will learn how to use a backslash (\) to disable the special meaning of a character.
[1]anchor: An anchor is a metacharacter such as ^ or $ that restricts a match to a particular position.
[2]shell variable: A shell variable is a place to store data that is used by the shell.