## 34.8. Delimiting a Regular Expression

Whether in sed or vi, when using the substitution command, a delimiter is required to separate the search pattern from the replacement string. The delimiter can be any character except blank or a newline (vi seems to be more restrictive than sed, although vim is extremely flexible). However, the usual practice is to use the slash (`/`) as a delimiter (for example, `s/``search``/`replacement`/`).

When either the search pattern or the replacement string contains a slash, it is easier to change the delimiter character than to escape the slash. Thus, if the pattern was attempting to match Unix pathnames, which contain slashes, you could choose another character, such as a colon, as the delimiter:

>     s:/usr/mail:/usr2/mail:

Note that the delimiter appears three times and is required after the replacement. Regardless of which delimiter you use, if it does appear in the search pattern or the replacement, put a backslash (`\`) before it to escape it.

If you don't know what characters the search pattern might have (in a shell program that handles any kind of input, for instance), the safest choice for the delimiter can be a control character.

You can use any delimiter for a pattern address (not just a slash). Put a backslash before the first delimiter. For example, to delete all lines containing /usr/mail, using a colon (:) as the delimiter:

>     \:/usr/mail:d

--DD and JP

Copyright © 2003 O'Reilly & Associates. All rights reserved.