perlre.html
来自「perl教程」· HTML 代码 · 共 1,102 行 · 第 1/5 页
HTML
1,102 行
<?xml version="1.0" ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<!-- saved from url=(0017)http://localhost/ -->
<script language="JavaScript" src="../../displayToc.js"></script>
<script language="JavaScript" src="../../tocParas.js"></script>
<script language="JavaScript" src="../../tocTab.js"></script>
<link rel="stylesheet" type="text/css" href="../../scineplex.css">
<title>perlre - Perl regular expressions</title>
<link rel="stylesheet" href="../../Active.css" type="text/css" />
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<link rev="made" href="mailto:" />
</head>
<body>
<script>writelinks('__top__',2);</script>
<h1><a>perlre - Perl regular expressions</a></h1>
<p><a name="__index__"></a></p>
<!-- INDEX BEGIN -->
<ul>
<li><a href="#name">NAME</a></li>
<li><a href="#description">DESCRIPTION</a></li>
<ul>
<li><a href="#regular_expressions">Regular Expressions</a></li>
<li><a href="#extended_patterns">Extended Patterns</a></li>
<li><a href="#backtracking">Backtracking</a></li>
<li><a href="#version_8_regular_expressions">Version 8 Regular Expressions</a></li>
<li><a href="#warning_on__1_vs__1">Warning on \1 vs $1</a></li>
<li><a href="#repeated_patterns_matching_zerolength_substring">Repeated patterns matching zero-length substring</a></li>
<li><a href="#combining_pieces_together">Combining pieces together</a></li>
<li><a href="#creating_custom_re_engines">Creating custom RE engines</a></li>
</ul>
<li><a href="#bugs">BUGS</a></li>
<li><a href="#see_also">SEE ALSO</a></li>
</ul>
<!-- INDEX END -->
<hr />
<p>
</p>
<h1><a name="name">NAME</a></h1>
<p>perlre - Perl regular expressions</p>
<p>
</p>
<hr />
<h1><a name="description">DESCRIPTION</a></h1>
<p>This page describes the syntax of regular expressions in Perl.</p>
<p>If you haven't used regular expressions before, a quick-start
introduction is available in <a href="../../lib/Pod/perlrequick.html">the perlrequick manpage</a>, and a longer tutorial
introduction is available in <a href="../../lib/Pod/perlretut.html">the perlretut manpage</a>.</p>
<p>For reference on how regular expressions are used in matching
operations, plus various examples of the same, see discussions of
<a href="../../lib/Pod/perlfunc.html#item_m_"><code>m//</code></a>, <a href="../../lib/Pod/perlfunc.html#item_s_"><code>s///</code></a>, <a href="../../lib/Pod/perlfunc.html#item_qr_"><code>qr//</code></a> and <code>??</code> in <a href="../../lib/Pod/perlop.html#regexp_quotelike_operators">Regexp Quote-Like Operators in the perlop manpage</a>.</p>
<p>Matching operations can have various modifiers. Modifiers
that relate to the interpretation of the regular expression inside
are listed below. Modifiers that alter the way a regular expression
is used by Perl are detailed in <a href="../../lib/Pod/perlop.html#regexp_quotelike_operators">Regexp Quote-Like Operators in the perlop manpage</a> and
<a href="../../lib/Pod/perlop.html#gory_details_of_parsing_quoted_constructs">Gory details of parsing quoted constructs in the perlop manpage</a>.</p>
<dl>
<dt><strong><a name="item_i">i</a></strong>
<dd>
<p>Do case-insensitive pattern matching.</p>
</dd>
<dd>
<p>If <code>use locale</code> is in effect, the case map is taken from the current
locale. See <a href="../../lib/Pod/perllocale.html">the perllocale manpage</a>.</p>
</dd>
</li>
<dt><strong><a name="item_m">m</a></strong>
<dd>
<p>Treat string as multiple lines. That is, change "^" and "$" from matching
the start or end of the string to matching the start or end of any
line anywhere within the string.</p>
</dd>
</li>
<dt><strong><a name="item_s">s</a></strong>
<dd>
<p>Treat string as single line. That is, change "." to match any character
whatsoever, even a newline, which normally it would not match.</p>
</dd>
<dd>
<p>The <code>/s</code> and <code>/m</code> modifiers both override the <a href="../../lib/Pod/perlvar.html#item___"><code>$*</code></a> setting. That
is, no matter what <a href="../../lib/Pod/perlvar.html#item___"><code>$*</code></a> contains, <code>/s</code> without <code>/m</code> will force
"^" to match only at the beginning of the string and "$" to match
only at the end (or just before a newline at the end) of the string.
Together, as /ms, they let the "." match any character whatsoever,
while still allowing "^" and "$" to match, respectively, just after
and just before newlines within the string.</p>
</dd>
</li>
<dt><strong><a name="item_x">x</a></strong>
<dd>
<p>Extend your pattern's legibility by permitting whitespace and comments.</p>
</dd>
</li>
</dl>
<p>These are usually written as "the <code>/x</code> modifier", even though the delimiter
in question might not really be a slash. Any of these
modifiers may also be embedded within the regular expression itself using
the <code>(?...)</code> construct. See below.</p>
<p>The <code>/x</code> modifier itself needs a little more explanation. It tells
the regular expression parser to ignore whitespace that is neither
backslashed nor within a character class. You can use this to break up
your regular expression into (slightly) more readable parts. The <code>#</code>
character is also treated as a metacharacter introducing a comment,
just as in ordinary Perl code. This also means that if you want real
whitespace or <code>#</code> characters in the pattern (outside a character
class, where they are unaffected by <code>/x</code>), that you'll either have to
escape them or encode them using octal or hex escapes. Taken together,
these features go a long way towards making Perl's regular expressions
more readable. Note that you have to be careful not to include the
pattern delimiter in the comment--perl has no way of knowing you did
not intend to close the pattern early. See the C-comment deletion code
in <a href="../../lib/Pod/perlop.html">the perlop manpage</a>.</p>
<p>
</p>
<h2><a name="regular_expressions">Regular Expressions</a></h2>
<p>The patterns used in Perl pattern matching derive from supplied in
the Version 8 regex routines. (The routines are derived
(distantly) from Henry Spencer's freely redistributable reimplementation
of the V8 routines.) See <a href="#version_8_regular_expressions">Version 8 Regular Expressions</a> for
details.</p>
<p>In particular the following metacharacters have their standard <em>egrep</em>-ish
meanings:</p>
<pre>
\ Quote the next metacharacter
^ Match the beginning of the line
. Match any character (except newline)
$ Match the end of the line (or before newline at the end)
| Alternation
() Grouping
[] Character class</pre>
<p>By default, the "^" character is guaranteed to match only the
beginning of the string, the "$" character only the end (or before the
newline at the end), and Perl does certain optimizations with the
assumption that the string contains only one line. Embedded newlines
will not be matched by "^" or "$". You may, however, wish to treat a
string as a multi-line buffer, such that the "^" will match after any
newline within the string, and "$" will match before any newline. At the
cost of a little more overhead, you can do this by using the /m modifier
on the pattern match operator. (Older programs did this by setting <a href="../../lib/Pod/perlvar.html#item___"><code>$*</code></a>,
but this practice is now deprecated.)</p>
<p>To simplify multi-line substitutions, the "." character never matches a
newline unless you use the <code>/s</code> modifier, which in effect tells Perl to pretend
the string is a single line--even if it isn't. The <code>/s</code> modifier also
overrides the setting of <a href="../../lib/Pod/perlvar.html#item___"><code>$*</code></a>, in case you have some (badly behaved) older
code that sets it in another module.</p>
<p>The following standard quantifiers are recognized:</p>
<pre>
* Match 0 or more times
+ Match 1 or more times
? Match 1 or 0 times
{n} Match exactly n times
{n,} Match at least n times
{n,m} Match at least n but not more than m times</pre>
<p>(If a curly bracket occurs in any other context, it is treated
as a regular character. In particular, the lower bound
is not optional.) The "*" modifier is equivalent to <code>{0,}</code>, the "+"
modifier to <code>{1,}</code>, and the "?" modifier to <code>{0,1}</code>. n and m are limited
to integral values less than a preset limit defined when perl is built.
This is usually 32766 on the most common platforms. The actual limit can
be seen in the error message generated by code such as this:</p>
<pre>
<span class="variable">$_</span> <span class="operator">**=</span> <span class="variable">$_</span> <span class="operator">,</span> <span class="regex">/ {$_} /</span> <span class="keyword">for</span> <span class="number">2</span> <span class="operator">..</span> <span class="number">42</span><span class="operator">;</span>
</pre>
<p>By default, a quantified subpattern is "greedy", that is, it will match as
many times as possible (given a particular starting location) while still
allowing the rest of the pattern to match. If you want it to match the
minimum number of times possible, follow the quantifier with a "?". Note
that the meanings don't change, just the "greediness":</p>
<pre>
*? Match 0 or more times
+? Match 1 or more times
?? Match 0 or 1 time
{n}? Match exactly n times
{n,}? Match at least n times
{n,m}? Match at least n but not more than m times</pre>
<p>Because patterns are processed as double quoted strings, the following
also work:</p>
<pre>
<span class="operator">\</span><span class="variable">t</span> <span class="variable">tab</span> <span class="operator">(</span><span class="variable">HT</span><span class="operator">,</span> <span class="variable">TAB</span><span class="operator">)</span>
<span class="operator">\</span><span class="variable">n</span> <span class="variable">newline</span> <span class="operator">(</span><span class="variable">LF</span><span class="operator">,</span> <span class="variable">NL</span><span class="operator">)</span>
<span class="operator">\</span><span class="variable">r</span> <span class="keyword">return</span> <span class="operator">(</span><span class="variable">CR</span><span class="operator">)</span>
<span class="operator">\</span><span class="variable">f</span> <span class="variable">form</span> <span class="variable">feed</span> <span class="operator">(</span><span class="variable">FF</span><span class="operator">)</span>
<span class="operator">\</span><span class="variable">a</span> <span class="keyword">alarm</span> <span class="operator">(</span><span class="variable">bell</span><span class="operator">)</span> <span class="operator">(</span><span class="variable">BEL</span><span class="operator">)</span>
<span class="operator">\</span><span class="variable">e</span> <span class="variable">escape</span> <span class="operator">(</span><span class="variable">think</span> <span class="variable">troff</span><span class="operator">)</span> <span class="operator">(</span><span class="variable">ESC</span><span class="operator">)</span>
<span class="operator">\</span><span class="number">033</span> <span class="variable">octal</span> <span class="variable">char</span> <span class="operator">(</span><span class="variable">think</span> <span class="variable">of</span> <span class="variable">a</span> <span class="variable">PDP</span><span class="operator">-</span><span class="number">11</span><span class="operator">)</span>
<span class="operator">\</span><span class="variable">x1B</span> <span class="keyword">hex</span> <span class="variable">char</span>
<span class="operator">\x</span><span class="operator">{</span><span class="string">263a</span><span class="operator">}</span> <span class="variable">wide</span> <span class="keyword">hex</span> <span class="variable">char</span> <span class="operator">(</span><span class="variable">Unicode</span> <span class="variable">SMILEY</span><span class="operator">)</span>
<span class="operator">\</span><span class="variable">c</span><span class="operator">[</span> <span class="variable">control</span> <span class="variable">char</span>
<span class="operator">\</span><span class="variable">N</span><span class="operator">{</span><span class="variable">name</span><span class="operator">}</span> <span class="variable">named</span> <span class="variable">char</span>
<span class="operator">\</span><span class="variable">l</span> <span class="variable">lowercase</span> <span class="keyword">next</span> <span class="variable">char</span> <span class="operator">(</span><span class="variable">think</span> <span class="variable">vi</span><span class="operator">)</span>
<span class="operator">\</span><span class="variable">u</span> <span class="variable">uppercase</span> <span class="keyword">next</span> <span class="variable">char</span> <span class="operator">(</span><span class="variable">think</span> <span class="variable">vi</span><span class="operator">)</span>
<span class="operator">\</span><span class="variable">L</span> <span class="variable">lowercase</span> <span class="variable">till</span> <span class="operator">\</span><span class="variable">E</span> <span class="operator">(</span><span class="variable">think</span> <span class="variable">vi</span><span class="operator">)</span>
<span class="operator">\</span><span class="variable">U</span> <span class="variable">uppercase</span> <span class="variable">till</span> <span class="operator">\</span><span class="variable">E</span> <span class="operator">(</span><span class="variable">think</span> <span class="variable">vi</span><span class="operator">)</span>
<span class="operator">\</span><span class="variable">E</span> <span class="variable">end</span> <span class="variable">case</span> <span class="variable">modification</span> <span class="operator">(</span><span class="variable">think</span> <span class="variable">vi</span><span class="operator">)</span>
<span class="operator">\</span><span class="variable">Q</span> <span class="variable">quote</span> <span class="operator">(</span><span class="variable">disable</span><span class="operator">)</span> <span class="variable">pattern</span> <span class="variable">metacharacters</span> <span class="variable">till</span> <span class="operator">\</span><span class="variable">E</span>
</pre>
<p>If <code>use locale</code> is in effect, the case map used by <code>\l</code>, <code>\L</code>, <code>\u</code>
and <code>\U</code> is taken from the current locale. See <a href="../../lib/Pod/perllocale.html">the perllocale manpage</a>. For
documentation of <code>\N{name}</code>, see <a href="../../lib/charnames.html">the charnames manpage</a>.</p>
<p>You cannot include a literal <code>$</code> or <code>@</code> within a <code>\Q</code> sequence.
An unescaped <code>$</code> or <code>@</code> interpolates the corresponding variable,
while escaping will cause the literal string <code>\$</code> to be matched.
You'll need to write something like <a href="../../lib/Pod/perlfunc.html#item_m_"><code>m/\Quser\E\@\Qhost/</code></a>.</p>
<p>In addition, Perl defines the following:</p>
<pre>
\w Match a "word" character (alphanumeric plus "_")
\W Match a non-"word" character
\s Match a whitespace character
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?