1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
|
Elvis 1.4 REGULAR EXPRESSIONS Page 4-1
E4. REGULAR EXPRESSIONSF
Elvis uses regular expressions for searching and substututions.
E4.1 SyntaxF
Elvis' regexp package treats the following one- or two-character
strings (called meta-characters) in special ways:
\( \) Used to delimit subexpressions
^ Matches the beginning of a line
$ Matches the end of a line
\< Matches the beginning of a word
\> Matches the end of a word
[ ] Matches any single character inside the brackets
* The preceding may be repeated 0 or more times
\+ The preceding may be repeated 1 or more times
\? The preceding is optional
Anything else is treated as a normal character which must match
exactly. The special strings may all be preceded by a backslash to
force them to be treated normally.
E4.2 OptionsF
Elvis has two options which affect the way regular expressions
are used. These options may be examined or set via the :set
command.
The first option is called "[no]magic". This is a boolean
option, and it is "magic" (TRUE) by default. While in magic mode,
all of the meta-characters behave as described above. In nomagic
mode, only ^ and $ retain their special meaning.
The second option is called "[no]ignorecase". This is a boolean
option, and it is "noignorecase" (FALSE) by default. While in
ignorecase mode, the searching mechanism will not distinguish
between an uppercase letter and its lowercase form. In
noignorecase mode, uppercase and lowercase are treated as being
different.
Also, the "[no]wrapscan" option affects searches.
E4.3 SubstitutionsF
The :s command has at least two arguments: a regular expression,
and a substitution string. The text that matched the regular
expression is replaced by text which is derived from the
substitution string.
Elvis 1.4 REGULAR EXPRESSIONS Page 4-2
Most characters in the substitution string are copied into the
text literally but a few have special meaning:
& Insert a copy of the original text
~ Insert a copy of the previous replacement text
\1 Insert a copy of that portion of the original text which
matched the first set of \( \) parentheses.
\2 - \9 Does the same for the second (etc.) pair of \( \).
\U Convert all chars of any later &, ~, or \# to uppercase
\L Convert all chars of any later &, ~, or \# to lowercase
\E End the effect of \U or \L
\u Convert the first char of the next &, ~ or \# to uppercase
\l Convert the first char of the next &, ~ or \# to lowercase
These may be preceded by a backslash to force them to be treated
normally. If "nomagic" mode is in effect, then & and ~ will be
treated normally, and you must write them as \& and \~ form them to
have special meaning.
E4.4 ExamplesF
This example changes every occurence of "utilize" to "use":
:%s/utilize/use/g
This example deletes all whitespace that occurs at the end of a
line anywhere in the file. (The brackets contain a single space
and a single tab.):
:%s/[ ]\+$//
This example converts the current line to uppercase:
:s/.*/\U&/
This example underlines each letter in the current line, by
changing it into an "underscore backspace letter" sequence. (The
^H is entered as "control-V backspace".):
:s/[a-zA-Z]/_^H&/g
This example locates the last colon in a line, and swaps the
text before the colon with the text after the colon. The first \(
\) pair is used to delineate the stuff before the colon, and the
second pair delineates the stuff after. In the substitution text,
\1 and \2 are given in reverse order, to perform the swap:
:s/\(.*\):\(.*\)/\2:\1/
|