6.3. Pattern-Matching Rules

In making global replacements, UNIX editors such as vi allow you to search not just for fixed strings of characters, but also for variable patterns of words, referred to as regular expressions.

When you specify a literal string of characters, the search might turn up other occurrences that you didn't want to match. The problem with searching for words in a file is that a word can be used in different ways. Regular expressions help you conduct a search for words in context. Note that regular expressions can be used with the vi search commands / and ? as well as in the ex :g and :s commands.

For the most part, the same regular expressions work with other UNIX programs such as grep, sed, and awk.[2]

[2] Much more information on regular expressions can be found in the two O'Reilly books sed & awk, by Dale Dougherty and Arnold Robbins, and Mastering Regular Expressions, by Jeffrey E.F. Friedl.

Regular expressions are made up by combining normal characters with a number of special characters called metacharacters.[3] The metacharacters and their uses are listed below.

[3] Technically speaking, we should probably call these metasequences, since sometimes two characters together have special meaning, and not just single characters. Nevertheless, the term metacharacters is in common use in UNIX literature, so we follow that convention here.

6.3.1. Metacharacters Used in Search Patterns

.

Matches any single character except a newline. Remember ...

Get Learning the vi Editor, Sixth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.