📄 _chapter 2.htm
字号:
</table>
<blockquote>
<p class="docFootnote"><sup><a name="ch02tabfn01">[a]</a></sup> Not dependable
on all versions of UNIX or all pattern-matching utilities; usually works with
<span class="docEmphasis">vi</span> and <span class="docEmphasis">grep</span>.</p>
</blockquote>
<p class="docText">Assuming that you know how the <span class="docEmphasis">vi</span>
editor works, each metacharacter is described in terms of the
<span class="docEmphasis">vi</span> search string. In the following examples,
characters are highlighted to demonstrate what <span class="docEmphasis">vi</span>
will find in its search.</p>
<h5 id="ch02list01" class="docExampleTitle">Example 2.1 </h5>
<pre>(A Simple Regular Expression Search)
% <span class="docEmphStrong">vi picnic</span>
----------------------------------------------------------------
I had a <span class="docEmphBoldItalic">love</span>ly time on our little picnic.
Lovers were all around us. It is springtime. Oh
<span class="docEmphBoldItalic">love,</span> how much I adore you. Do you know
the extent of my <span class="docEmphBoldItalic">love?</span> Oh, by the way, I think
I lost my g<span class="docEmphBoldItalic">love</span>s somewhere out in that field of
c<span class="docEmphBoldItalic">love</span>r. Did you see them? I can only hope <span class="docEmphBoldItalic">love</span>
is forever. I live for you. It's hard to get back in the
groove.
~
~
~
<span class="docEmphStrong">/love/</span>
-----------------------------------------------------------------
</pre>
<div align="center">
<center>
<table cellSpacing="0" width="90%" border="1" style="border-collapse: collapse" bordercolor="#111111" cellpadding="5">
<tr>
<td>
<h2 class="docSidebarTitle">EXPLANATION</h2>
<p class="docText">The regular expression is <span class="docEmphasis">love</span>.
The pattern <span class="docEmphasis">love</span> is found by itself and as
part of other words, such as <span class="docEmphasis">lovely</span>,
<span class="docEmphasis">gloves</span>, and <span class="docEmphasis">
clover</span>.</td>
</tr>
</table>
</center>
</div>
<h5 id="ch02list02" class="docExampleTitle">Example 2.2 </h5>
<pre>(The Beginning-of-Line Anchor (^))
% <span class="docEmphStrong">vi picnic</span>
----------------------------------------------------------------
I had a lovely time on our little picnic.
Lovers were all around us. It is springtime. Oh
<span class="docEmphBoldItalic">love,</span> how much I adore you. Do you know
the extent of my love? Oh, by the way, I think
I lost my gloves somewhere out in that field of
clover. Did you see them? I can only hope love
is forever. I live for you. It's hard to get back in the
groove.
~
~
~
<span class="docEmphStrong">/^love/</span>
-----------------------------------------------------------------
</pre>
<div align="center">
<center>
<table cellSpacing="0" width="90%" border="1" style="border-collapse: collapse" bordercolor="#111111" cellpadding="5">
<tr>
<td>
<h2 class="docSidebarTitle">EXPLANATION</h2>
<p class="docText">The caret (<span class="docEmphasis">^</span>) is called
the beginning-of-line anchor. <span class="docEmphasis">Vi</span> will find
only those lines where the regular expression <span class="docEmphasis">love</span>
is matched at the beginning of the line, i.e., <span class="docEmphasis">
love</span> is the first set of characters on the line; it cannot be
preceded by even one space.</td>
</tr>
</table>
</center>
</div>
<h5 id="ch02list03" class="docExampleTitle">Example 2.3 </h5>
<pre>(The End-of-Line Anchor ($))
% <span class="docEmphStrong">vi picnic</span>
----------------------------------------------------------------
I had a lovely time on our little picnic.
Lovers were all around us. It is springtime. Oh
love, how much I adore you. Do you know
the extent of my love? Oh, by the way, I think
I lost my gloves somewhere out in that field of
clover. Did you see them? I can only hope <span class="docEmphBoldItalic">love</span>
is forever. I live for you. It's hard to get back in the
groove.
~
~
~
<span class="docEmphStrong">/love$/</span>
----------------------------------------------------------------
</pre>
<div align="center">
<center>
<table cellSpacing="0" width="90%" border="1" style="border-collapse: collapse" bordercolor="#111111" cellpadding="5">
<tr>
<td>
<h2 class="docSidebarTitle">EXPLANATION</h2>
<p class="docText">The dollar sign (<span class="docEmphasis">$</span>) is
called the end-of-line anchor. <span class="docEmphasis">Vi</span> will find
only those lines where the regular expression <span class="docEmphasis">love</span>
is matched at the end of the line, i.e., <span class="docEmphasis">love</span>
is the last set of characters on the line and is directly followed by a
newline.</td>
</tr>
</table>
</center>
</div>
<h5 id="ch02list04" class="docExampleTitle">Example 2.4 </h5>
<pre>(Any Single Character (.))
% <span class="docEmphStrong">vi picnic</span>
----------------------------------------------------------------
I had a <span class="docEmphBoldItalic">love</span>ly time on our little picnic.
Lovers were all around us. It is springtime. Oh
<span class="docEmphBoldItalic">love,</span> how much I adore you. Do you know
the extent of my <span class="docEmphBoldItalic">love</span>? Oh, by the way, I think
I lost my g<span class="docEmphBoldItalic">love</span>s somewhere out in that field of
c<span class="docEmphBoldItalic">love</span>r. Did you see them? I can only hope <span class="docEmphBoldItalic">love</span>
is forever. I <span class="docEmphBoldItalic">live</span> for you. It's hard to get back in the
groove.
~
~
~
<span class="docEmphStrong">/l.ve/</span>
-----------------------------------------------------------------
</pre>
<div align="center">
<center>
<table cellSpacing="0" width="90%" border="1" style="border-collapse: collapse" bordercolor="#111111" cellpadding="5">
<tr>
<td>
<h2 class="docSidebarTitle">EXPLANATION</h2>
<p class="docText">The dot (.) matches any one character, except the newline.
<span class="docEmphasis">Vi</span> will find those lines where the regular
expression consists of an <span class="docEmphasis">l</span>, followed by
any single character, followed by a <span class="docEmphasis">v</span> and
an <span class="docEmphasis">e</span>. It finds combinations of
<span class="docEmphasis">love</span> and <span class="docEmphasis">live</span>.</td>
</tr>
</table>
</center>
</div>
<h5 id="ch02list05" class="docExampleTitle">Example 2.5 </h5>
<pre>(Zero or More of the Preceding Character (*))
% <span class="docEmphStrong">vi picnic</span>
----------------------------------------------------------------
I had a l<span class="docEmphBoldItalic">ove</span>ly time on our little picnic.
L<span class="docEmphBoldItalic">ove</span>rs were all around us. It is springtime. Oh
l<span class="docEmphBoldItalic">ove,</span> how much I adore you. Do you know
the extent of my l<span class="docEmphBoldItalic">ove</span>? Oh, by the way, I think
I lost my gl<span class="docEmphBoldItalic">ove</span>s somewhere out in that field of
clover. Did you see them? I can only hope l<span class="docEmphBoldItalic">ove</span>
is forever. I live for you. It's hard to get back in the
gr<span class="docEmphBoldItalic">oove.</span>
~
~
~
<span class="docEmphStrong">/o*ve/</span>
-----------------------------------------------------------------
</pre>
<div align="center">
<center>
<table cellSpacing="0" width="90%" border="1" style="border-collapse: collapse" bordercolor="#111111" cellpadding="5">
<tr>
<td>
<h2 class="docSidebarTitle">EXPLANATION</h2>
<p class="docText">The asterisk (*) matches zero or more of the preceding
character.<span id="ENB2-2"><a class="docLink" href="#EN2-2"><sup>[2]</sup></a></span>
It is as though the asterisk were glued to the character directly before it
and controls only that character. In this case, the asterisk is glued to the
letter <span class="docEmphasis">o.</span> It matches for only the letter
<span class="docEmphasis">o</span> and as many consecutive occurrences of
the letter <span class="docEmphasis">o</span> as there are in the pattern,
even no occurrences of <span class="docEmphasis">o</span> at all.
<span class="docEmphasis">Vi</span> searches for zero or more occurrences of
the letter <span class="docEmphasis">o</span> followed by a
<span class="docEmphasis">v</span> and an <span class="docEmphasis">e,</span>
finding <span class="docEmphasis">love, loooove, lve</span>, and so forth.</td>
</tr>
</table>
</center>
</div>
<h5 id="ch02list06" class="docExampleTitle">Example 2.6 </h5>
<pre>(A Set of Characters ([]))
% <span class="docEmphStrong">vi picnic</span>
----------------------------------------------------------------
I had a <span class="docEmphBoldItalic">love</span>ly time on our little picnic.
<span class="docEmphBoldItalic">Love</span>rs were all around us. It is springtime. Oh
<span class="docEmphBoldItalic">love,</span> how much I adore you. Do you know
the extent of my <span class="docEmphBoldItalic">love</span>? Oh, by the way, I think
I lost my g<span class="docEmphBoldItalic">love</span>s somewhere out in that field of
c<span class="docEmphBoldItalic">love</span>r. Did you see them? I can only hope <span class="docEmphBoldItalic">love</span>
is forever. I live for you. It's hard to get back in the
groove.
~
~
~
<span class="docEmphStrong">/[Ll]ove/</span>
----------------------------------------------------------------
</pre>
<div align="center">
<center>
<table cellSpacing="0" width="90%" border="1" style="border-collapse: collapse" bordercolor="#111111" cellpadding="5">
<tr>
<td>
<h2 class="docSidebarTitle">EXPLANATION</h2>
<p class="docText">The square brackets match for one of a set of characters.
<span class="docEmphasis">Vi</span> will search for the regular expression
containing either an uppercase or lowercase <span class="docEmphasis">l</span>
followed by an <span class="docEmphasis">o,</span> <span class="docEmphasis">
v</span>, and <span class="docEmphasis">e</span>.</td>
</tr>
</table>
</center>
</div>
<h5 id="ch02list07" class="docExampleTitle">Example 2.7 </h5>
<pre>(A Range of Characters ( [ - ] ))
% <span class="docEmphStrong">vi picnic</span>
----------------------------------------------------------------
I had a l<span class="docEmphBoldItalic">ovel</span>y time on our little picnic.
L<span class="docEmphBoldItalic">over</span>s were all around us. It is springtime. Oh
love, how much I adore you. Do you know
the extent of my love? Oh, by the way, I think
I lost my gl<span class="docEmphBoldItalic">oves</span> somewhere out in that field of
cl<span class="docEmphBoldItalic">over.</span> Did you see them? I can only hope love
is forever. I live for you. It's hard to get back in the
groove.
~
~
~
<span class="docEmphStrong">/ove[a-z]/</span>
-----------------------------------------------------------------
</pre>
<div align="center">
<center>
<table cellSpacing="0" width="90%" border="1" style="border-collapse: collapse" bordercolor="#111111" cellpadding="5">
<tr>
<td>
<h2 class="docSidebarTitle">EXPLANATION</h2>
<p class="docText">The dash between characters enclosed in square brackets
matches one character in a range of characters. <span class="docEmphasis">Vi</span>
will search for the regular expression containing an
<span class="docEmphasis">o,</span> <span class="docEmphasis">v,</span> and
<span class="docEmphasis">e,</span> followed by any character in the ASCII
range between <span class="docEmphasis">a</span> and
<span class="docEmphasis">z.</span> Since this is an ASCII range, the range
cannot be represented as [<span class="docEmphasis">z</span>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -