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