perlrequick.html
来自「perl教程」· HTML 代码 · 共 557 行 · 第 1/3 页
HTML
557 行
<p><code>a{n,m}</code> = match at least <a href="../../lib/Pod/perlguts.html#item_n"><code>n</code></a> times, but not more than <a href="../../lib/Pod/perlguts.html#item_m"><code>m</code></a>
times.</p>
</li>
<li>
<p><code>a{n,}</code> = match at least <a href="../../lib/Pod/perlguts.html#item_n"><code>n</code></a> or more times</p>
</li>
<li>
<p><code>a{n}</code> = match exactly <a href="../../lib/Pod/perlguts.html#item_n"><code>n</code></a> times</p>
</li>
</ul>
<p>Here are some examples:</p>
<pre>
<span class="regex">/[a-z]+\s+\d*/</span><span class="operator">;</span> <span class="comment"># match a lowercase word, at least some space, and</span>
<span class="comment"># any number of digits</span>
<span class="regex">/(\w+)\s+\1/</span><span class="operator">;</span> <span class="comment"># match doubled words of arbitrary length</span>
<span class="variable">$year</span> <span class="operator">=~</span> <span class="regex">/\d{2,4}/</span><span class="operator">;</span> <span class="comment"># make sure year is at least 2 but not more</span>
<span class="comment"># than 4 digits</span>
<span class="variable">$year</span> <span class="operator">=~</span> <span class="regex">/\d{4}|\d{2}/</span><span class="operator">;</span> <span class="comment"># better match; throw out 3 digit dates</span>
</pre>
<p>These quantifiers will try to match as much of the string as possible,
while still allowing the regex to match. So we have</p>
<pre>
<span class="variable">$x</span> <span class="operator">=</span> <span class="string">'the cat in the hat'</span><span class="operator">;</span>
<span class="variable">$x</span> <span class="operator">=~</span> <span class="regex">/^(.*)(at)(.*)$/</span><span class="operator">;</span> <span class="comment"># matches,</span>
<span class="comment"># $1 = 'the cat in the h'</span>
<span class="comment"># $2 = 'at'</span>
<span class="comment"># $3 = '' (0 matches)</span>
</pre>
<p>The first quantifier <code>.*</code> grabs as much of the string as possible
while still having the regex match. The second quantifier <code>.*</code> has
no string left to it, so it matches 0 times.</p>
<p>
</p>
<h2><a name="more_matching">More matching</a></h2>
<p>There are a few more things you might want to know about matching
operators. In the code</p>
<pre>
<span class="variable">$pattern</span> <span class="operator">=</span> <span class="string">'Seuss'</span><span class="operator">;</span>
<span class="keyword">while</span> <span class="operator">(<>)</span> <span class="operator">{</span>
<span class="keyword">print</span> <span class="keyword">if</span> <span class="regex">/$pattern/</span><span class="operator">;</span>
<span class="operator">}</span>
</pre>
<p>perl has to re-evaluate <code>$pattern</code> each time through the loop. If
<code>$pattern</code> won't be changing, use the <code>//o</code> modifier, to only
perform variable substitutions once. If you don't want any
substitutions at all, use the special delimiter <code>m''</code>:</p>
<pre>
<span class="variable">@pattern</span> <span class="operator">=</span> <span class="operator">(</span><span class="string">'Seuss'</span><span class="operator">);</span>
<span class="regex">m/@pattern/</span><span class="operator">;</span> <span class="comment"># matches 'Seuss'</span>
<span class="regex">m'@pattern'</span><span class="operator">;</span> <span class="comment"># matches the literal string '@pattern'</span>
</pre>
<p>The global modifier <code>//g</code> allows the matching operator to match
within a string as many times as possible. In scalar context,
successive matches against a string will have <code>//g</code> jump from match
to match, keeping track of position in the string as it goes along.
You can get or set the position with the <a href="../../lib/Pod/perlfunc.html#item_pos"><code>pos()</code></a> function.
For example,</p>
<pre>
<span class="variable">$x</span> <span class="operator">=</span> <span class="string">"cat dog house"</span><span class="operator">;</span> <span class="comment"># 3 words</span>
<span class="keyword">while</span> <span class="operator">(</span><span class="variable">$x</span> <span class="operator">=~</span> <span class="regex">/(\w+)/g</span><span class="operator">)</span> <span class="operator">{</span>
<span class="keyword">print</span> <span class="string">"Word is $1, ends at position "</span><span class="operator">,</span> <span class="keyword">pos</span> <span class="variable">$x</span><span class="operator">,</span> <span class="string">"\n"</span><span class="operator">;</span>
<span class="operator">}</span>
</pre>
<p>prints</p>
<pre>
Word is cat, ends at position 3
Word is dog, ends at position 7
Word is house, ends at position 13</pre>
<p>A failed match or changing the target string resets the position. If
you don't want the position reset after failure to match, add the
<code>//c</code>, as in <code>/regex/gc</code>.</p>
<p>In list context, <code>//g</code> returns a list of matched groupings, or if
there are no groupings, a list of matches to the whole regex. So</p>
<pre>
<span class="variable">@words</span> <span class="operator">=</span> <span class="operator">(</span><span class="variable">$x</span> <span class="operator">=~</span> <span class="regex">/(\w+)/g</span><span class="operator">);</span> <span class="comment"># matches,</span>
<span class="comment"># $word[0] = 'cat'</span>
<span class="comment"># $word[1] = 'dog'</span>
<span class="comment"># $word[2] = 'house'</span>
</pre>
<p>
</p>
<h2><a name="search_and_replace">Search and replace</a></h2>
<p>Search and replace is performed using <a href="../../lib/Pod/perlfunc.html#item_s_"><code>s/regex/replacement/modifiers</code></a>.
The <code>replacement</code> is a Perl double quoted string that replaces in the
string whatever is matched with the <code>regex</code>. The operator <code>=~</code> is
also used here to associate a string with <a href="../../lib/Pod/perlfunc.html#item_s_"><code>s///</code></a>. If matching
against <a href="../../lib/Pod/perlvar.html#item___"><code>$_</code></a>, the <a href="../../lib/Pod/perlvar.html#item___"><code>$_ =~</code></a> can be dropped. If there is a match,
<a href="../../lib/Pod/perlfunc.html#item_s_"><code>s///</code></a> returns the number of substitutions made, otherwise it returns
false. Here are a few examples:</p>
<pre>
<span class="variable">$x</span> <span class="operator">=</span> <span class="string">"Time to feed the cat!"</span><span class="operator">;</span>
<span class="variable">$x</span> <span class="operator">=~</span> <span class="regex">s/cat/hacker/</span><span class="operator">;</span> <span class="comment"># $x contains "Time to feed the hacker!"</span>
<span class="variable">$y</span> <span class="operator">=</span> <span class="string">"'quoted words'"</span><span class="operator">;</span>
<span class="variable">$y</span> <span class="operator">=~</span> <span class="regex">s/^'(.*)'$/$1/</span><span class="operator">;</span> <span class="comment"># strip single quotes,</span>
<span class="comment"># $y contains "quoted words"</span>
</pre>
<p>With the <a href="../../lib/Pod/perlfunc.html#item_s_"><code>s///</code></a> operator, the matched variables <code>$1</code>, <code>$2</code>, etc.
are immediately available for use in the replacement expression. With
the global modifier, <a href="../../lib/Pod/perlfunc.html#item_s_"><code>s///g</code></a> will search and replace all occurrences
of the regex in the string:</p>
<pre>
<span class="variable">$x</span> <span class="operator">=</span> <span class="string">"I batted 4 for 4"</span><span class="operator">;</span>
<span class="variable">$x</span> <span class="operator">=~</span> <span class="regex">s/4/four/</span><span class="operator">;</span> <span class="comment"># $x contains "I batted four for 4"</span>
<span class="variable">$x</span> <span class="operator">=</span> <span class="string">"I batted 4 for 4"</span><span class="operator">;</span>
<span class="variable">$x</span> <span class="operator">=~</span> <span class="regex">s/4/four/g</span><span class="operator">;</span> <span class="comment"># $x contains "I batted four for four"</span>
</pre>
<p>The evaluation modifier <a href="../../lib/Pod/perlfunc.html#item_s_"><code>s///e</code></a> wraps an <a href="../../lib/Pod/perlfunc.html#item_eval"><code>eval{...}</code></a> around the
replacement string and the evaluated result is substituted for the
matched substring. Some examples:</p>
<pre>
<span class="comment"># reverse all the words in a string</span>
<span class="variable">$x</span> <span class="operator">=</span> <span class="string">"the cat in the hat"</span><span class="operator">;</span>
<span class="variable">$x</span> <span class="operator">=~</span> <span class="regex">s/(\w+)/reverse $1/ge</span><span class="operator">;</span> <span class="comment"># $x contains "eht tac ni eht tah"</span>
</pre>
<pre>
<span class="comment"># convert percentage to decimal</span>
<span class="variable">$x</span> <span class="operator">=</span> <span class="string">"A 39% hit rate"</span><span class="operator">;</span>
<span class="variable">$x</span> <span class="operator">=~</span> <span class="regex">s!(\d+)%!$1/100!e</span><span class="operator">;</span> <span class="comment"># $x contains "A 0.39 hit rate"</span>
</pre>
<p>The last example shows that <a href="../../lib/Pod/perlfunc.html#item_s_"><code>s///</code></a> can use other delimiters, such as
<code>s!!!</code> and <a href="../../lib/Pod/perlguts.html#item_s"><code>s{}{}</code></a>, and even <code>s{}//</code>. If single quotes are used
<code>s'''</code>, then the regex and replacement are treated as single quoted
strings.</p>
<p>
</p>
<h2><a name="the_split_operator">The split operator</a></h2>
<p><code>split /regex/, string</code> splits <code>string</code> into a list of substrings
and returns that list. The regex determines the character sequence
that <code>string</code> is split with respect to. For example, to split a
string into words, use</p>
<pre>
<span class="variable">$x</span> <span class="operator">=</span> <span class="string">"Calvin and Hobbes"</span><span class="operator">;</span>
<span class="variable">@word</span> <span class="operator">=</span> <span class="keyword">split</span> <span class="regex">/\s+/</span><span class="operator">,</span> <span class="variable">$x</span><span class="operator">;</span> <span class="comment"># $word[0] = 'Calvin'</span>
<span class="comment"># $word[1] = 'and'</span>
<span class="comment"># $word[2] = 'Hobbes'</span>
</pre>
<p>To extract a comma-delimited list of numbers, use</p>
<pre>
<span class="variable">$x</span> <span class="operator">=</span> <span class="string">"1.618,2.718, 3.142"</span><span class="operator">;</span>
<span class="variable">@const</span> <span class="operator">=</span> <span class="keyword">split</span> <span class="regex">/,\s*/</span><span class="operator">,</span> <span class="variable">$x</span><span class="operator">;</span> <span class="comment"># $const[0] = '1.618'</span>
<span class="comment"># $const[1] = '2.718'</span>
<span class="comment"># $const[2] = '3.142'</span>
</pre>
<p>If the empty regex <code>//</code> is used, the string is split into individual
characters. If the regex has groupings, then the list produced contains
the matched substrings from the groupings as well:</p>
<pre>
<span class="variable">$x</span> <span class="operator">=</span> <span class="string">"/usr/bin"</span><span class="operator">;</span>
<span class="variable">@parts</span> <span class="operator">=</span> <span class="keyword">split</span> <span class="regex">m!(/)!</span><span class="operator">,</span> <span class="variable">$x</span><span class="operator">;</span> <span class="comment"># $parts[0] = ''</span>
<span class="comment"># $parts[1] = '/'</span>
<span class="comment"># $parts[2] = 'usr'</span>
<span class="comment"># $parts[3] = '/'</span>
<span class="comment"># $parts[4] = 'bin'</span>
</pre>
<p>Since the first character of $x matched the regex, <a href="../../lib/Pod/perlfunc.html#item_split"><code>split</code></a> prepended
an empty initial element to the list.</p>
<p>
</p>
<hr />
<h1><a name="bugs">BUGS</a></h1>
<p>None.</p>
<p>
</p>
<hr />
<h1><a name="see_also">SEE ALSO</a></h1>
<p>This is just a quick start guide. For a more in-depth tutorial on
regexes, see <a href="../../lib/Pod/perlretut.html">the perlretut manpage</a> and for the reference page, see <a href="../../lib/Pod/perlre.html">the perlre manpage</a>.</p>
<p>
</p>
<hr />
<h1><a name="author_and_copyright">AUTHOR AND COPYRIGHT</a></h1>
<p>Copyright (c) 2000 Mark Kvale
All rights reserved.</p>
<p>This document may be distributed under the same terms as Perl itself.</p>
<p>
</p>
<h2><a name="acknowledgments">Acknowledgments</a></h2>
<p>The author would like to thank Mark-Jason Dominus, Tom Christiansen,
Ilya Zakharevich, Brad Hughes, and Mike Giroux for all their helpful
comments.</p>
</body>
</html>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?