perlretut.html

来自「perl教程」· HTML 代码 · 共 919 行 · 第 1/5 页

HTML
919 行
字号
without trouble.</p>
<p>Let's consider how different regexps would match <code>&quot;Hello World&quot;</code>:</p>
<pre>
    <span class="string">"Hello World"</span> <span class="operator">=~</span> <span class="regex">/world/</span><span class="operator">;</span>  <span class="comment"># doesn't match</span>
    <span class="string">"Hello World"</span> <span class="operator">=~</span> <span class="regex">/o W/</span><span class="operator">;</span>    <span class="comment"># matches</span>
    <span class="string">"Hello World"</span> <span class="operator">=~</span> <span class="regex">/oW/</span><span class="operator">;</span>     <span class="comment"># doesn't match</span>
    <span class="string">"Hello World"</span> <span class="operator">=~</span> <span class="regex">/World /</span><span class="operator">;</span> <span class="comment"># doesn't match</span>
</pre>
<p>The first regexp <code>world</code> doesn't match because regexps are
case-sensitive.  The second regexp matches because the substring
<code>'o&nbsp;W'</code>&nbsp; occurs in the string <code>&quot;Hello&nbsp;World&quot;</code>&nbsp;.  The space
character ' ' is treated like any other character in a regexp and is
needed to match in this case.  The lack of a space character is the
reason the third regexp <code>'oW'</code> doesn't match.  The fourth regexp
<code>'World '</code> doesn't match because there is a space at the end of the
regexp, but not at the end of the string.  The lesson here is that
regexps must match a part of the string <em>exactly</em> in order for the
statement to be true.</p>
<p>If a regexp matches in more than one place in the string, perl will
always match at the earliest possible point in the string:</p>
<pre>
    <span class="string">"Hello World"</span> <span class="operator">=~</span> <span class="regex">/o/</span><span class="operator">;</span>       <span class="comment"># matches 'o' in 'Hello'</span>
    <span class="string">"That hat is red"</span> <span class="operator">=~</span> <span class="regex">/hat/</span><span class="operator">;</span> <span class="comment"># matches 'hat' in 'That'</span>
</pre>
<p>With respect to character matching, there are a few more points you
need to know about.   First of all, not all characters can be used 'as
is' in a match.  Some characters, called <strong>metacharacters</strong>, are reserved
for use in regexp notation.  The metacharacters are</p>
<pre>
    <span class="operator">{}</span><span class="operator">[]</span><span class="operator">()^</span><span class="variable">$.</span><span class="operator">|*+</span><span class="regex">?\
    </span>
</pre>
<p>The significance of each of these will be explained
in the rest of the tutorial, but for now, it is important only to know
that a metacharacter can be matched by putting a backslash before it:</p>
<pre>
    <span class="string">"2+2=4"</span> <span class="operator">=~</span> <span class="regex">/2+2/</span><span class="operator">;</span>    <span class="comment"># doesn't match, + is a metacharacter</span>
    <span class="string">"2+2=4"</span> <span class="operator">=~</span> <span class="regex">/2\+2/</span><span class="operator">;</span>   <span class="comment"># matches, \+ is treated like an ordinary +</span>
    <span class="string">"The interval is [0,1)."</span> <span class="operator">=~</span> <span class="regex">/[0,1)./</span>     <span class="comment"># is a syntax error!</span>
    <span class="string">"The interval is [0,1)."</span> <span class="operator">=~</span> <span class="regex">/\[0,1\)\./</span>  <span class="comment"># matches</span>
    <span class="string">"/usr/bin/perl"</span> <span class="operator">=~</span> <span class="regex">/\/usr\/bin\/perl/</span><span class="operator">;</span>  <span class="comment"># matches</span>
</pre>
<p>In the last regexp, the forward slash <code>'/'</code> is also backslashed,
because it is used to delimit the regexp.  This can lead to LTS
(leaning toothpick syndrome), however, and it is often more readable
to change delimiters.</p>
<pre>
    <span class="string">"/usr/bin/perl"</span> <span class="operator">=~</span> <span class="regex">m!/usr/bin/perl!</span><span class="operator">;</span>    <span class="comment"># easier to read</span>
</pre>
<p>The backslash character <code>'\'</code> is a metacharacter itself and needs to
be backslashed:</p>
<pre>
    <span class="string">'C:\WIN32'</span> <span class="operator">=~</span> <span class="regex">/C:\\WIN/</span><span class="operator">;</span>   <span class="comment"># matches</span>
</pre>
<p>In addition to the metacharacters, there are some ASCII characters
which don't have printable character equivalents and are instead
represented by <strong>escape sequences</strong>.  Common examples are <code>\t</code> for a
tab, <code>\n</code> for a newline, <code>\r</code> for a carriage return and <code>\a</code> for a
bell.  If your string is better thought of as a sequence of arbitrary
bytes, the octal escape sequence, e.g., <code>\033</code>, or hexadecimal escape
sequence, e.g., <code>\x1B</code> may be a more natural representation for your
bytes.  Here are some examples of escapes:</p>
<pre>
    &quot;1000\t2000&quot; =~ m(0\t2)   # matches
    &quot;1000\n2000&quot; =~ /0\n20/   # matches
    &quot;1000\t2000&quot; =~ /\000\t2/ # doesn't match, &quot;0&quot; ne &quot;\000&quot;
    &quot;cat&quot;        =~ /\143\x61\x74/ # matches, but a weird way to spell cat</pre>
<p>If you've been around Perl a while, all this talk of escape sequences
may seem familiar.  Similar escape sequences are used in double-quoted
strings and in fact the regexps in Perl are mostly treated as
double-quoted strings.  This means that variables can be used in
regexps as well.  Just like double-quoted strings, the values of the
variables in the regexp will be substituted in before the regexp is
evaluated for matching purposes.  So we have:</p>
<pre>
    <span class="variable">$foo</span> <span class="operator">=</span> <span class="string">'house'</span><span class="operator">;</span>
    <span class="string">'housecat'</span> <span class="operator">=~</span> <span class="regex">/$foo/</span><span class="operator">;</span>      <span class="comment"># matches</span>
    <span class="string">'cathouse'</span> <span class="operator">=~</span> <span class="regex">/cat$foo/</span><span class="operator">;</span>   <span class="comment"># matches</span>
    <span class="string">'housecat'</span> <span class="operator">=~</span> <span class="regex">/${foo}cat/</span><span class="operator">;</span> <span class="comment"># matches</span>
</pre>
<p>So far, so good.  With the knowledge above you can already perform
searches with just about any literal string regexp you can dream up.
Here is a <em>very simple</em> emulation of the Unix grep program:</p>
<pre>
    <span class="operator">% </span><span class="variable">cat</span> <span class="operator">&gt;</span> <span class="variable">simple_grep</span>
    <span class="comment">#!/usr/bin/perl</span>
    <span class="variable">$regexp</span> <span class="operator">=</span> <span class="keyword">shift</span><span class="operator">;</span>
    <span class="keyword">while</span> <span class="operator">(&lt;&gt;)</span> <span class="operator">{</span>
        <span class="keyword">print</span> <span class="keyword">if</span> <span class="regex">/$regexp/</span><span class="operator">;</span>
    <span class="operator">}</span>
    <span class="operator">^</span><span class="variable">D</span>
</pre>
<pre>
    % chmod +x simple_grep</pre>
<pre>
    % simple_grep abba /usr/dict/words
    Babbage
    cabbage
    cabbages
    sabbath
    Sabbathize
    Sabbathizes
    sabbatical
    scabbard
    scabbards</pre>
<p>This program is easy to understand.  <code>#!/usr/bin/perl</code> is the standard
way to invoke a perl program from the shell.
<code>$regexp&nbsp;=&nbsp;shift;</code>&nbsp; saves the first command line argument as the
regexp to be used, leaving the rest of the command line arguments to
be treated as files.  <code>while&nbsp;(&lt;&gt;)</code>&nbsp; loops over all the lines in
all the files.  For each line, <code>print&nbsp;if&nbsp;/$regexp/;</code>&nbsp; prints the
line if the regexp matches the line.  In this line, both <a href="../../lib/Pod/perlfunc.html#item_print"><code>print</code></a> and
<code>/$regexp/</code> use the default variable <a href="../../lib/Pod/perlvar.html#item___"><code>$_</code></a> implicitly.</p>
<p>With all of the regexps above, if the regexp matched anywhere in the
string, it was considered a match.  Sometimes, however, we'd like to
specify <em>where</em> in the string the regexp should try to match.  To do
this, we would use the <strong>anchor</strong> metacharacters <code>^</code> and <code>$</code>.  The
anchor <code>^</code> means match at the beginning of the string and the anchor
<code>$</code> means match at the end of the string, or before a newline at the
end of the string.  Here is how they are used:</p>
<pre>
    <span class="string">"housekeeper"</span> <span class="operator">=~</span> <span class="regex">/keeper/</span><span class="operator">;</span>    <span class="comment"># matches</span>
    <span class="string">"housekeeper"</span> <span class="operator">=~</span> <span class="regex">/^keeper/</span><span class="operator">;</span>   <span class="comment"># doesn't match</span>
    <span class="string">"housekeeper"</span> <span class="operator">=~</span> <span class="regex">/keeper$/</span><span class="operator">;</span>   <span class="comment"># matches</span>
    <span class="string">"housekeeper\n"</span> <span class="operator">=~</span> <span class="regex">/keeper$/</span><span class="operator">;</span> <span class="comment"># matches</span>
</pre>
<p>The second regexp doesn't match because <code>^</code> constrains <code>keeper</code> to
match only at the beginning of the string, but <code>&quot;housekeeper&quot;</code> has
keeper starting in the middle.  The third regexp does match, since the
<code>$</code> constrains <code>keeper</code> to match only at the end of the string.</p>
<p>When both <code>^</code> and <code>$</code> are used at the same time, the regexp has to
match both the beginning and the end of the string, i.e., the regexp
matches the whole string.  Consider</p>
<pre>
    <span class="string">"keeper"</span> <span class="operator">=~</span> <span class="regex">/^keep$/</span><span class="operator">;</span>      <span class="comment"># doesn't match</span>
    <span class="string">"keeper"</span> <span class="operator">=~</span> <span class="regex">/^keeper$/</span><span class="operator">;</span>    <span class="comment"># matches</span>
    <span class="string">""</span>       <span class="operator">=~</span> <span class="regex">/^$/</span><span class="operator">;</span>          <span class="comment"># ^$ matches an empty string</span>
</pre>
<p>The first regexp doesn't match because the string has more to it than
<code>keep</code>.  Since the second regexp is exactly the string, it
matches.  Using both <code>^</code> and <code>$</code> in a regexp forces the complete
string to match, so it gives you complete control over which strings
match and which don't.  Suppose you are looking for a fellow named
bert, off in a string by himself:</p>
<pre>
    <span class="string">"dogbert"</span> <span class="operator">=~</span> <span class="regex">/bert/</span><span class="operator">;</span>   <span class="comment"># matches, but not what you want</span>
</pre>
<pre>
    <span class="string">"dilbert"</span> <span class="operator">=~</span> <span class="regex">/^bert/</span><span class="operator">;</span>  <span class="comment"># doesn't match, but ..</span>
    <span class="string">"bertram"</span> <span class="operator">=~</span> <span class="regex">/^bert/</span><span class="operator">;</span>  <span class="comment"># matches, so still not good enough</span>
</pre>
<pre>
    <span class="string">"bertram"</span> <span class="operator">=~</span> <span class="regex">/^bert$/</span><span class="operator">;</span> <span class="comment"># doesn't match, good</span>
    <span class="string">"dilbert"</span> <span class="operator">=~</span> <span class="regex">/^bert$/</span><span class="operator">;</span> <span class="comment"># doesn't match, good</span>
    <span class="string">"bert"</span>    <span class="operator">=~</span> <span class="regex">/^bert$/</span><span class="operator">;</span> <span class="comment"># matches, perfect</span>
</pre>
<p>Of course, in the case of a literal string, one could just as easily
use the string equivalence <code>$string&nbsp;eq&nbsp;'bert'</code>&nbsp; and it would be
more efficient.   The  <code>^...$</code> regexp really becomes useful when we
add in the more powerful regexp tools below.</p>
<p>
</p>
<h2><a name="using_character_classes">Using character classes</a></h2>
<p>Although one can already do quite a lot with the literal string
regexps above, we've only scratched the surface of regular expression
technology.  In this and subsequent sections we will introduce regexp
concepts (and associated metacharacter notations) that will allow a
regexp to not just represent a single character sequence, but a <em>whole
class</em> of them.</p>
<p>One such concept is that of a <strong>character class</strong>.  A character class
allows a set of possible characters, rather than just a single
character, to match at a particular point in a regexp.  Character
classes are denoted by brackets <code>[...]</code>, with the set of characters
to be possibly matched inside.  Here are some examples:</p>
<pre>
    <span class="regex">/cat/</span><span class="operator">;</span>       <span class="comment"># matches 'cat'</span>
    <span class="regex">/[bcr]at/</span><span class="operator">;</span>   <span class="comment"># matches 'bat, 'cat', or 'rat'</span>
    <span class="regex">/item[0123456789]/</span><span class="operator">;</span>  <span class="comment"># matches 'item0' or ... or 'item9'</span>
    <span class="string">"abc"</span> <span class="operator">=~</span> <span class="regex">/[cab]/</span><span class="operator">;</span>    <span class="comment"># matches 'a'</span>
</pre>
<p>In the last statement, even though <code>'c'</code> is the first character in
the class, <code>'a'</code> matches because the first character position in the
string is the earliest point at which the regexp can match.</p>
<pre>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?