perlretut.html
来自「perl教程」· HTML 代码 · 共 919 行 · 第 1/5 页
HTML
919 行
<span class="regex">/[yY][eE][sS]/</span><span class="operator">;</span> <span class="comment"># match 'yes' in a case-insensitive way</span>
<span class="comment"># 'yes', 'Yes', 'YES', etc.</span>
</pre>
<p>This regexp displays a common task: perform a case-insensitive
match. Perl provides a way of avoiding all those brackets by simply
appending an <code>'i'</code> to the end of the match. Then <code>/[yY][eE][sS]/;</code>
can be rewritten as <code>/yes/i;</code>. The <code>'i'</code> stands for
case-insensitive and is an example of a <strong>modifier</strong> of the matching
operation. We will meet other modifiers later in the tutorial.</p>
<p>We saw in the section above that there were ordinary characters, which
represented themselves, and special characters, which needed a
backslash <code>\</code> to represent themselves. The same is true in a
character class, but the sets of ordinary and special characters
inside a character class are different than those outside a character
class. The special characters for a character class are <code>-]\^$</code>. <code>]</code>
is special because it denotes the end of a character class. <code>$</code> is
special because it denotes a scalar variable. <code>\</code> is special because
it is used in escape sequences, just like above. Here is how the
special characters <code>]$\</code> are handled:</p>
<pre>
<span class="regex">/[\]c]def/</span><span class="operator">;</span> <span class="comment"># matches ']def' or 'cdef'</span>
<span class="variable">$x</span> <span class="operator">=</span> <span class="string">'bcr'</span><span class="operator">;</span>
<span class="regex">/[$x]at/</span><span class="operator">;</span> <span class="comment"># matches 'bat', 'cat', or 'rat'</span>
<span class="regex">/[\$x]at/</span><span class="operator">;</span> <span class="comment"># matches '$at' or 'xat'</span>
<span class="regex">/[\\$x]at/</span><span class="operator">;</span> <span class="comment"># matches '\at', 'bat, 'cat', or 'rat'</span>
</pre>
<p>The last two are a little tricky. in <code>[\$x]</code>, the backslash protects
the dollar sign, so the character class has two members <code>$</code> and <a href="../../lib/Pod/perlguts.html#item_x"><code>x</code></a>.
In <code>[\\$x]</code>, the backslash is protected, so <code>$x</code> is treated as a
variable and substituted in double quote fashion.</p>
<p>The special character <code>'-'</code> acts as a range operator within character
classes, so that a contiguous set of characters can be written as a
range. With ranges, the unwieldy <code>[0123456789]</code> and <code>[abc...xyz]</code>
become the svelte <code>[0-9]</code> and <code>[a-z]</code>. Some examples are</p>
<pre>
<span class="regex">/item[0-9]/</span><span class="operator">;</span> <span class="comment"># matches 'item0' or ... or 'item9'</span>
<span class="regex">/[0-9bx-z]aa/</span><span class="operator">;</span> <span class="comment"># matches '0aa', ..., '9aa',</span>
<span class="comment"># 'baa', 'xaa', 'yaa', or 'zaa'</span>
<span class="regex">/[0-9a-fA-F]/</span><span class="operator">;</span> <span class="comment"># matches a hexadecimal digit</span>
<span class="regex">/[0-9a-zA-Z_]/</span><span class="operator">;</span> <span class="comment"># matches a "word" character,</span>
<span class="comment"># like those in a perl variable name</span>
</pre>
<p>If <code>'-'</code> is the first or last character in a character class, it is
treated as an ordinary character; <code>[-ab]</code>, <code>[ab-]</code> and <code>[a\-b]</code> are
all equivalent.</p>
<p>The special character <code>^</code> in the first position of a character class
denotes a <strong>negated character class</strong>, which matches any character but
those in the brackets. Both <code>[...]</code> and <code>[^...]</code> must match a
character, or the match fails. Then</p>
<pre>
<span class="regex">/[^a]at/</span><span class="operator">;</span> <span class="comment"># doesn't match 'aat' or 'at', but matches</span>
<span class="comment"># all other 'bat', 'cat, '0at', '%at', etc.</span>
<span class="regex">/[^0-9]/</span><span class="operator">;</span> <span class="comment"># matches a non-numeric character</span>
<span class="regex">/[a^]at/</span><span class="operator">;</span> <span class="comment"># matches 'aat' or '^at'; here '^' is ordinary</span>
</pre>
<p>Now, even <code>[0-9]</code> can be a bother to write multiple times, so in the
interest of saving keystrokes and making regexps more readable, Perl
has several abbreviations for common character classes:</p>
<ul>
<li>
<p>\d is a digit and represents [0-9]</p>
</li>
<li>
<p>\s is a whitespace character and represents [\ \t\r\n\f]</p>
</li>
<li>
<p>\w is a word character (alphanumeric or _) and represents [0-9a-zA-Z_]</p>
</li>
<li>
<p>\D is a negated \d; it represents any character but a digit [^0-9]</p>
</li>
<li>
<p>\S is a negated \s; it represents any non-whitespace character [^\s]</p>
</li>
<li>
<p>\W is a negated \w; it represents any non-word character [^\w]</p>
</li>
<li>
<p>The period '.' matches any character but "\n"</p>
</li>
</ul>
<p>The <code>\d\s\w\D\S\W</code> abbreviations can be used both inside and outside
of character classes. Here are some in use:</p>
<pre>
<span class="regex">/\d\d:\d\d:\d\d/</span><span class="operator">;</span> <span class="comment"># matches a hh:mm:ss time format</span>
<span class="regex">/[\d\s]/</span><span class="operator">;</span> <span class="comment"># matches any digit or whitespace character</span>
<span class="regex">/\w\W\w/</span><span class="operator">;</span> <span class="comment"># matches a word char, followed by a</span>
<span class="comment"># non-word char, followed by a word char</span>
<span class="regex">/..rt/</span><span class="operator">;</span> <span class="comment"># matches any two chars, followed by 'rt'</span>
<span class="regex">/end\./</span><span class="operator">;</span> <span class="comment"># matches 'end.'</span>
<span class="regex">/end[.]/</span><span class="operator">;</span> <span class="comment"># same thing, matches 'end.'</span>
</pre>
<p>Because a period is a metacharacter, it needs to be escaped to match
as an ordinary period. Because, for example, <code>\d</code> and <code>\w</code> are sets
of characters, it is incorrect to think of <code>[^\d\w]</code> as <code>[\D\W]</code>; in
fact <code>[^\d\w]</code> is the same as <code>[^\w]</code>, which is the same as
<code>[\W]</code>. Think De Morgan's laws.</p>
<p>An anchor useful in basic regexps is the <strong>word anchor</strong>
<code>\b</code>. This matches a boundary between a word character and a non-word
character <code>\w\W</code> or <code>\W\w</code>:</p>
<pre>
<span class="variable">$x</span> <span class="operator">=</span> <span class="string">"Housecat catenates house and cat"</span><span class="operator">;</span>
<span class="variable">$x</span> <span class="operator">=~</span> <span class="regex">/cat/</span><span class="operator">;</span> <span class="comment"># matches cat in 'housecat'</span>
<span class="variable">$x</span> <span class="operator">=~</span> <span class="regex">/\bcat/</span><span class="operator">;</span> <span class="comment"># matches cat in 'catenates'</span>
<span class="variable">$x</span> <span class="operator">=~</span> <span class="regex">/cat\b/</span><span class="operator">;</span> <span class="comment"># matches cat in 'housecat'</span>
<span class="variable">$x</span> <span class="operator">=~</span> <span class="regex">/\bcat\b/</span><span class="operator">;</span> <span class="comment"># matches 'cat' at end of string</span>
</pre>
<p>Note in the last example, the end of the string is considered a word
boundary.</p>
<p>You might wonder why <code>'.'</code> matches everything but <code>"\n"</code> - why not
every character? The reason is that often one is matching against
lines and would like to ignore the newline characters. For instance,
while the string <code>"\n"</code> represents one line, we would like to think
of it as empty. Then</p>
<pre>
<span class="string">""</span> <span class="operator">=~</span> <span class="regex">/^$/</span><span class="operator">;</span> <span class="comment"># matches</span>
<span class="string">"\n"</span> <span class="operator">=~</span> <span class="regex">/^$/</span><span class="operator">;</span> <span class="comment"># matches, "\n" is ignored</span>
</pre>
<pre>
<span class="string">""</span> <span class="operator">=~</span> <span class="regex">/./</span><span class="operator">;</span> <span class="comment"># doesn't match; it needs a char</span>
<span class="string">""</span> <span class="operator">=~</span> <span class="regex">/^.$/</span><span class="operator">;</span> <span class="comment"># doesn't match; it needs a char</span>
<span class="string">"\n"</span> <span class="operator">=~</span> <span class="regex">/^.$/</span><span class="operator">;</span> <span class="comment"># doesn't match; it needs a char other than "\n"</span>
<span class="string">"a"</span> <span class="operator">=~</span> <span class="regex">/^.$/</span><span class="operator">;</span> <span class="comment"># matches</span>
<span class="string">"a\n"</span> <span class="operator">=~</span> <span class="regex">/^.$/</span><span class="operator">;</span> <span class="comment"># matches, ignores the "\n"</span>
</pre>
<p>This behavior is convenient, because we usually want to ignore
newlines when we count and match characters in a line. Sometimes,
however, we want to keep track of newlines. We might even want <code>^</code>
and <code>$</code> to anchor at the beginning and end of lines within the
string, rather than just the beginning and end of the string. Perl
allows us to choose between ignoring and paying attention to newlines
by using the <code>//s</code> and <code>//m</code> modifiers. <code>//s</code> and <code>//m</code> stand for
single line and multi-line and they determine whether a string is to
be treated as one continuous string, or as a set of lines. The two
modifiers affect two aspects of how the regexp is interpreted: 1) how
the <code>'.'</code> character class is defined, and 2) where the anchors <code>^</code>
and <code>$</code> are able to match. Here are the four possible combinations:</p>
<ul>
<li>
<p>no modifiers (//): Default behavior. <code>'.'</code> matches any character
except <code>"\n"</code>. <code>^</code> matches only at the beginning of the string and
<code>$</code> matches only at the end or before a newline at the end.</p>
</li>
<li>
<p>s modifier (//s): Treat string as a single long line. <code>'.'</code> matches
any character, even <code>"\n"</code>. <code>^</code> matches only at the beginning of
the string and <code>$</code> matches only at the end or before a newline at the
end.</p>
</li>
<li>
<p>m modifier (//m): Treat string as a set of multiple lines. <code>'.'</code>
matches any character except <code>"\n"</code>. <code>^</code> and <code>$</code> are able to match
at the start or end of <em>any</em> line within the string.</p>
</li>
<li>
<p>both s and m modifiers (//sm): Treat string as a single long line, but
detect multiple lines. <code>'.'</code> matches any character, even
<code>"\n"</code>. <code>^</code> and <code>$</code>, however, are able to match at the start or end
of <em>any</em> line within the string.</p>
</li>
</ul>
<p>Here are examples of <code>//s</code> and <code>//m</code> in action:</p>
<pre>
<span class="variable">$x</span> <span class="operator">=</span> <span class="string">"There once was a girl\nWho programmed in Perl\n"</span><span class="operator">;</span>
</pre>
<pre>
<span class="variable">$x</span> <span class="operator">=~</span> <span class="regex">/^Who/</span><span class="operator">;</span> <span class="comment"># doesn't match, "Who" not at start of string</span>
<span class="variable">$x</span> <span class="operator">=~</span> <span class="regex">/^Who/s</span><span class="operator">;</span> <span class="comment"># doesn't match, "Who" not at start of string</span>
<span class="variable">$x</span> <span class="operator">=~</span> <span class="regex">/^Who/m</span><span class="operator">;</span> <span class="comment"># matches, "Who" at start of second line</span>
<span class="variable">$x</span> <span class="operator">=~</span> <span class="regex">/^Who/sm</span><span class="operator">;</span> <span class="comment"># matches, "Who" at start of second line</span>
</pre>
<pre>
<span class="variable">$x</span> <span class="operator">=~</span> <span class="regex">/girl.Who/</span><span class="operator">;</span> <span class="comment"># doesn't match, "." doesn't match "\n"</span>
<span class="variable">$x</span> <span class="operator">=~</span> <span class="regex">/girl.Who/s</span><span class="operator">;</span> <span class="comment"># matches, "." matches "\n"</span>
<span class="variable">$x</span> <span class="operator">=~</span> <span class="regex">/girl.Who/m</span><span class="operator">;</span> <span class="comment"># doesn't match, "." doesn't match "\n"</span>
<span class="variable">$x</span> <span class="operator">=~</span> <span class="regex">/girl.Who/sm</span><span class="operator">;</span> <span class="comment"># matches, "." matches "\n"</span>
</pre>
<p>Most of the time, the default behavior is what is wanted, but <code>//s</code> and
<code>//m</code> are occasionally very useful. If <code>//m</code> is being used, the start
of the string can still be matched with <code>\A</code> and the end of the string
can still be matched with the anchors <code>\Z</code> (matches both the end and
the newline before, like <code>$</code>), and <code>\z</code> (matches only the end):</p>
<pre>
<span class="variable">$x</span> <span class="operator">=~</span> <span class="regex">/^Who/m</span><span class="operator">;</span> <span class="comment"># matches, "Who" at start of second line</span>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?