Regular Expressions   «Prev  Next»
Lesson 6 Using a backslash to turn off a character’s meaning
Objective Create regular expressions using the backslash ( \ ).

Using Backslash to turn off Character's Meaning

Some characters retain their special meaning within quotes, whether single or double quotes. One example is the exclamation point (!), used in command history. Other examples include the regular expression metacharacters themselves. For instance, you may want to search for a literal asterisk character. When quotes alone do not help, you can use a backslash (\). By placing a \ before a character, you disable any special meaning it has.
Another issue is how to search for quotes, because quotes themselves have special meaning in forming arguments. When a regular expression contains a double quote, enclose the expression in single quotes. When searching for items that contain a single quote, enclose the expression in double quotes.
The following Slide Show shows examples of searches using the backslash and other quoting situations.

Handling special cases in Regular Expressions

Using the cat command to display the sample file named text
1) Using the cat command to display the sample file named text

The C shell interprets an ! as the command history character, even within quotes. 
This command uses bad syntax and produces an "Event not found" error.
2) The C shell interprets an ! as the command history character, even within quotes. This command uses bad syntax and produces an "Event not found" error.

To correct the previous command, use \! instead. Now the search succeeds.
3) To correct the previous command, use \! instead. Now the search succeeds.

This command is meant to search for the title Mr., but it actually finds Mr. followed by any character. The period (.) operates as a regular expression inside the quotes, and two lines match.
4) This command is meant to search for the title Mr., but it actually finds Mr. followed by any character. The period (.) operates as a regular expression inside the quotes, and two lines match.

To search for a literal period, use \. . In the regular expression. Now the search finds just one matching line.
5) To search for a literal period, use \. . In the regular expression. Now the search finds just one matching line.

If a regular expression contains a single quote such as the word isn't <br> surround the expression with double quotes.
6) If a regular expression contains a single quote such as the word isn't
surround the expression with double quotes.

If you single-quote a regular expression that already contains a single quote, a syntax error occurs.
7) If you single-quote a regular expression that already contains a single quote, a syntax error occurs.

If a regular expression contains a double quote, as in the pattern "Hello" , surround the expression with single quotes. 
Because the is treated as a literal character, a matching is not needed. However, you need a matching pair of single quotes because they are used to enclose the argument
8) If a regular expression contains a double quote, as in the pattern "Hello" , surround the expression with single quotes. Because the is treated as a literal character, a matching is not needed. However, you need a matching pair of single quotes because they are used to enclose the argument


In the next lesson, you will wrap up this module by reviewing key commands, terms, and concepts you have learned, and then take a quiz.

Grep Command - Exercise

Click the Exercise link below to practice using grep with regular expressions.
Grep Command - Exercise