A regular expression specifies a set of strings of characters. A member of this set of strings is said to be matched by the regular expression. Ex remembers two previous regular expressions: the previous regular expression used in a substitute command and the previous regular expression used elsewhere (referred to as the previous scanning regular expression.) The previous regular expression can always be referred to by a null re, e.g. `//' or `??'.
The regular expressions allowed by ex are constructed in one of two ways depending on the setting of the magic option. The ex and vi default setting of magic gives quick access to a powerful set of regular expression metacharacters. The disadvantage of magic is that the user must remember that these metacharacters are magic and precede them with the character `\' to use them as ``ordinary'' characters. With nomagic, the default for edit, regular expressions are much simpler, there being only two metacharacters. The power of the other metacharacters is still available by preceding the (now) ordinary character with a `\'. Note that `\' is thus always a metacharacter.
The remainder of the discussion of regular expressions assumes that that the setting of this option is magic.**
The following basic constructs are used to construct magic mode regular expressions.
The concatenation of two regular expressions matches the leftmost and then longest string which can be divided with the first piece matching the first regular expression and the second piece matching the second. Any of the (single character matching) regular expressions mentioned above may be followed by the character `*' to form a regular expression which matches any number of adjacent occurrences (including 0) of characters matched by the regular expression it follows.
The character `~' may be used in a regular expression, and matches the text which defined the replacement part of the last substitute command. A regular expression may be enclosed between the sequences `\(' and `\)' with side effects in the substitute replacement patterns.
The basic metacharacters for the replacement pattern are `&' and `~'; these are given as `\&' and `\~' when nomagic is set. Each instance of `&' is replaced by the characters which the regular expression matched. The metacharacter `~' stands, in the replacement pattern, for the defining text of the previous replacement pattern.
Other metasequences possible in the replacement pattern are always introduced by the escaping character `\'. The sequence `\n' is replaced by the text matched by the n-th regular subexpression enclosed between `\(' and `\)'.** The sequences `\u' and `\l' cause the immediately following character in the replacement to be converted to upper- or lower-case respectively if this character is a letter. The sequences `\U' and `\L' turn such conversion on, either until `\E' or `\e' is encountered, or until the end of the replacement pattern.