Book HomeLearning the vi EditorSearch this book

8.4. Extended Regular Expressions

The metacharacters available in vi's search and substitution regular expressions are described in Section 6.3.1 in Chapter 6. Each of the clones provides some form of extended regular expressions, either as an option or always available. Typically these are the same (or almost the same) as what's provided by egrep. Unfortunately, each one's extended flavor is slightly different from the others'.

To give you a feel for what extended regular expressions can do, we present them in the context of nvi. Each clone's chapter then describes that editor's extended syntax, without repeating the examples.

nvi extended regular expressions are the Extended Regular Expressions (EREs) as defined by the POSIX standard. In order to enable this feature, use set extended from either your .nexrc file or from the ex colon prompt.

Besides the standard metacharacters described in Chapter 6, and the POSIX bracket expressions mentioned in Section 6.3.2 in the same chapter, the following metacharacters are available:

|
Indicates alternation. For example, a|b matches either a or b. However, this construct is not limited to single characters: house|home matches either of the strings house or home.

(...)
Used for grouping, to allow the application of additional regular expression operators. For example, house|home can be shortened (if not simplified) to ho(use|me). The * operator can be applied to text in parentheses: (house|home)* matches home, homehouse, househomehousehouse and so on.

When extended is set, text grouped with parentheses acts like text grouped in \(...\) in regular vi; the actual text matched can be retrieved in the replacement part of a substitute command with \1, \2, etc. In this case, \( represents a literal left parenthesis.

+
Matches one or more of the preceding regular expressions. This is either a single character, or a group of characters enclosed in parentheses. Note the difference between + and *. The * is allowed to match nothing, but with + there must be at least one match. For example, ho(use|me)* matches ho as well as home and house, but ho(use|me)+ will not match ho.

?
Matches zero or one occurrence of the preceding regular expression. This indicates "optional" text that is either present or not present. For example, free?d will match either fred or freed, but nothing else.

{...}
Defines an interval expression. Interval expressions describe counted numbers of repetitions. In the description below, n and m represent integer constants.

{n}
Matches exactly n repetitions of the previous regular expression. For example, (home|house){2} matches homehome, homehouse, househome, and househouse, but nothing else.

{n,}
Matches n or more repetitions of the previous regular expression. Think of it as "as least n" repititions.

{n,m}
Matches n to m repititions. The bounding is important, since it controls how much text would be replaced during a substitute command.[42]

[42]The *, +, and ? operators can be reduced to {0,}, {1,} and {0,1} respectively, but they are much more convenient to use.

When extended is not set, nvi provides the same functionality with \{ and \}.



Library Navigation Links

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