perlrequick.html

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

HTML
557 行
字号
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> and are matched using an
escape:</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 special character <code>'-'</code> acts as a range operator within character
classes, so that the unwieldy <code>[0123456789]</code> and <code>[abc...xyz]</code>
become the svelte <code>[0-9]</code> and <code>[a-z]</code>:</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-9a-fA-F]/</span><span class="operator">;</span>  <span class="comment"># matches a hexadecimal digit</span>
</pre>
<p>If <code>'-'</code> is the first or last character in a character class, it is
treated as an ordinary character.</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>Perl has several abbreviations for common character classes:</p>
<ul>
<li>
<p>\d is a digit and represents</p>
<pre>
    [0-9]</pre>
</li>
<li>
<p>\s is a whitespace character and represents</p>
<pre>
    [\ \t\r\n\f]</pre>
</li>
<li>
<p>\w is a word character (alphanumeric or _) and represents</p>
<pre>
    [0-9a-zA-Z_]</pre>
</li>
<li>
<p>\D is a negated \d; it represents any character but a digit</p>
<pre>
    [^0-9]</pre>
</li>
<li>
<p>\S is a negated \s; it represents any non-whitespace character</p>
<pre>
    [^\s]</pre>
</li>
<li>
<p>\W is a negated \w; it represents any non-word character</p>
<pre>
    [^\w]</pre>
</li>
<li>
<p>The period '.' matches any character but &quot;\n&quot;</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>The <strong>word&nbsp;anchor</strong>&nbsp; <code>\b</code> 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">/\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>In the last example, the end of the string is considered a word
boundary.</p>
<p>
</p>
<h2><a name="matching_this_or_that">Matching this or that</a></h2>
<p>We can match different character strings with the <strong>alternation</strong>
metacharacter <code>'|'</code>.  To match <code>dog</code> or <code>cat</code>, we form the regex
<code>dog|cat</code>.  As before, perl will try to match the regex at the
earliest possible point in the string.  At each character position,
perl will first try to match the first alternative, <code>dog</code>.  If
<code>dog</code> doesn't match, perl will then try the next alternative, <code>cat</code>.
If <code>cat</code> doesn't match either, then the match fails and perl moves to
the next position in the string.  Some examples:</p>
<pre>
    <span class="string">"cats and dogs"</span> <span class="operator">=~</span> <span class="regex">/cat|dog|bird/</span><span class="operator">;</span>  <span class="comment"># matches "cat"</span>
    <span class="string">"cats and dogs"</span> <span class="operator">=~</span> <span class="regex">/dog|cat|bird/</span><span class="operator">;</span>  <span class="comment"># matches "cat"</span>
</pre>
<p>Even though <code>dog</code> is the first alternative in the second regex,
<code>cat</code> is able to match earlier in the string.</p>
<pre>
    <span class="string">"cats"</span>          <span class="operator">=~</span> <span class="regex">/c|ca|cat|cats/</span><span class="operator">;</span> <span class="comment"># matches "c"</span>
    <span class="string">"cats"</span>          <span class="operator">=~</span> <span class="regex">/cats|cat|ca|c/</span><span class="operator">;</span> <span class="comment"># matches "cats"</span>
</pre>
<p>At a given character position, the first alternative that allows the
regex match to succeed will be the one that matches. Here, all the
alternatives match at the first string position, so the first matches.</p>
<p>
</p>
<h2><a name="grouping_things_and_hierarchical_matching">Grouping things and hierarchical matching</a></h2>
<p>The <strong>grouping</strong> metacharacters <code>()</code> allow a part of a regex to be
treated as a single unit.  Parts of a regex are grouped by enclosing
them in parentheses.  The regex <code>house(cat|keeper)</code> means match
<code>house</code> followed by either <code>cat</code> or <code>keeper</code>.  Some more examples
are</p>
<pre>
    <span class="regex">/(a|b)b/</span><span class="operator">;</span>    <span class="comment"># matches 'ab' or 'bb'</span>
    <span class="regex">/(^a|b)c/</span><span class="operator">;</span>   <span class="comment"># matches 'ac' at start of string or 'bc' anywhere</span>
</pre>
<pre>
    <span class="regex">/house(cat|)/</span><span class="operator">;</span>  <span class="comment"># matches either 'housecat' or 'house'</span>
    <span class="regex">/house(cat(s|)|)/</span><span class="operator">;</span>  <span class="comment"># matches either 'housecats' or 'housecat' or</span>
                        <span class="comment"># 'house'.  Note groups can be nested.</span>
</pre>
<pre>
    <span class="string">"20"</span> <span class="operator">=~</span> <span class="regex">/(19|20|)\d\d/</span><span class="operator">;</span>  <span class="comment"># matches the null alternative '()\d\d',</span>
                             <span class="comment"># because '20\d\d' can't match</span>
</pre>
<p>
</p>
<h2><a name="extracting_matches">Extracting matches</a></h2>
<p>The grouping metacharacters <code>()</code> also allow the extraction of the
parts of a string that matched.  For each grouping, the part that
matched inside goes into the special variables <code>$1</code>, <code>$2</code>, etc.
They can be used just as ordinary variables:</p>
<pre>
    <span class="comment"># extract hours, minutes, seconds</span>
    <span class="variable">$time</span> <span class="operator">=~</span> <span class="regex">/(\d\d):(\d\d):(\d\d)/</span><span class="operator">;</span>  <span class="comment"># match hh:mm:ss format</span>
    <span class="variable">$hours</span> <span class="operator">=</span> <span class="variable">$1</span><span class="operator">;</span>
    <span class="variable">$minutes</span> <span class="operator">=</span> <span class="variable">$2</span><span class="operator">;</span>
    <span class="variable">$seconds</span> <span class="operator">=</span> <span class="variable">$3</span><span class="operator">;</span>
</pre>
<p>In list context, a match <code>/regex/</code> with groupings will return the
list of matched values <code>($1,$2,...)</code>.  So we could rewrite it as</p>
<pre>
    <span class="operator">(</span><span class="variable">$hours</span><span class="operator">,</span> <span class="variable">$minutes</span><span class="operator">,</span> <span class="variable">$second</span><span class="operator">)</span> <span class="operator">=</span> <span class="operator">(</span><span class="variable">$time</span> <span class="operator">=~</span> <span class="regex">/(\d\d):(\d\d):(\d\d)/</span><span class="operator">);</span>
</pre>
<p>If the groupings in a regex are nested, <code>$1</code> gets the group with the
leftmost opening parenthesis, <code>$2</code> the next opening parenthesis,
etc.  For example, here is a complex regex and the matching variables
indicated below it:</p>
<pre>
    <span class="regex">/(ab(cd|ef)((gi)|j))/</span><span class="operator">;</span>
     <span class="number">1</span>  <span class="number">2</span>      <span class="number">34</span>
</pre>
<p>Associated with the matching variables <code>$1</code>, <code>$2</code>, ... are
the <strong>backreferences</strong> <code>\1</code>, <code>\2</code>, ...  Backreferences are
matching variables that can be used <em>inside</em> a regex:</p>
<pre>
    <span class="regex">/(\w\w\w)\s\1/</span><span class="operator">;</span> <span class="comment"># find sequences like 'the the' in string</span>
</pre>
<p><code>$1</code>, <code>$2</code>, ... should only be used outside of a regex, and <code>\1</code>,
<code>\2</code>, ... only inside a regex.</p>
<p>
</p>
<h2><a name="matching_repetitions">Matching repetitions</a></h2>
<p>The <strong>quantifier</strong> metacharacters <code>?</code>, <code>*</code>, <code>+</code>, and <code>{}</code> allow us
to determine the number of repeats of a portion of a regex we
consider to be a match.  Quantifiers are put immediately after the
character, character class, or grouping that we want to specify.  They
have the following meanings:</p>
<ul>
<li>
<p><code>a?</code> = match 'a' 1 or 0 times</p>
</li>
<li>
<p><code>a*</code> = match 'a' 0 or more times, i.e., any number of times</p>
</li>
<li>
<p><code>a+</code> = match 'a' 1 or more times, i.e., at least once</p>
</li>
<li>

⌨️ 快捷键说明

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