perlreref.html
来自「perl教程」· HTML 代码 · 共 366 行 · 第 1/2 页
HTML
366 行
</pre>
<p>POSIX character classes and their Unicode and Perl equivalents:</p>
<pre>
alnum IsAlnum Alphanumeric
alpha IsAlpha Alphabetic
ascii IsASCII Any ASCII char
blank IsSpace [ \t] Horizontal whitespace (GNU extension)
cntrl IsCntrl Control characters
digit IsDigit \d Digits
graph IsGraph Alphanumeric and punctuation
lower IsLower Lowercase chars (locale and Unicode aware)
print IsPrint Alphanumeric, punct, and space
punct IsPunct Punctuation
space IsSpace [\s\ck] Whitespace
IsSpacePerl \s Perl's whitespace definition
upper IsUpper Uppercase chars (locale and Unicode aware)
word IsWord \w Alphanumeric plus _ (Perl extension)
xdigit IsXDigit [0-9A-Fa-f] Hexadecimal digit</pre>
<p>Within a character class:</p>
<pre>
<span class="variable">POSIX</span> <span class="variable">traditional</span> <span class="variable">Unicode</span>
<span class="operator">[</span><span class="operator">:</span><span class="variable">digit</span><span class="operator">:</span><span class="operator">]</span> <span class="operator">\</span><span class="variable">d</span> <span class="operator">\</span><span class="variable">p</span><span class="operator">{</span><span class="variable">IsDigit</span><span class="operator">}</span>
<span class="operator">[</span><span class="operator">:^</span><span class="variable">digit</span><span class="operator">:</span><span class="operator">]</span> <span class="operator">\</span><span class="variable">D</span> <span class="operator">\</span><span class="variable">P</span><span class="operator">{</span><span class="variable">IsDigit</span><span class="operator">}</span>
</pre>
<p>
</p>
<h2><a name="anchors">ANCHORS</a></h2>
<p>All are zero-width assertions.</p>
<pre>
^ Match string start (or line, if /m is used)
$ Match string end (or line, if /m is used) or before newline
\b Match word boundary (between \w and \W)
\B Match except at word boundary (between \w and \w or \W and \W)
\A Match string start (regardless of /m)
\Z Match string end (before optional newline)
\z Match absolute string end
\G Match where previous m//g left off</pre>
<p>
</p>
<h2><a name="quantifiers">QUANTIFIERS</a></h2>
<p>Quantifiers are greedy by default -- match the <strong>longest</strong> leftmost.</p>
<pre>
Maximal Minimal Allowed range
------- ------- -------------
{n,m} {n,m}? Must occur at least n times but no more than m times
{n,} {n,}? Must occur at least n times
{n} {n}? Must occur exactly n times
* *? 0 or more times (same as {0,})
+ +? 1 or more times (same as {1,})
? ?? 0 or 1 time (same as {0,1})</pre>
<p>There is no quantifier {,n} -- that gets understood as a literal string.</p>
<p>
</p>
<h2><a name="extended_constructs">EXTENDED CONSTRUCTS</a></h2>
<pre>
<span class="operator">(</span><span class="regex">?#text) A comment
(?imxs</span><span class="operator">-</span><span class="variable">imsx</span><span class="operator">:...)</span> <span class="variable">Enable</span><span class="operator">/</span><span class="variable">disable</span> <span class="variable">option</span> <span class="operator">(</span><span class="variable">as</span> <span class="variable">per</span> <span class="regex">m//</span> <span class="variable">modifiers</span><span class="operator">)</span>
<span class="operator">(</span><span class="regex">?=...) Zero-width positive lookahead assertion
(?</span><span class="operator">!...)</span> <span class="variable">Zero</span><span class="operator">-</span><span class="variable">width</span> <span class="variable">negative</span> <span class="variable">lookahead</span> <span class="variable">assertion</span>
<span class="operator">(</span><span class="regex">?<=...) Zero-width positive lookbehind assertion
(?</span><span class="operator"><!...)</span> <span class="variable">Zero</span><span class="operator">-</span><span class="variable">width</span> <span class="variable">negative</span> <span class="variable">lookbehind</span> <span class="variable">assertion</span>
<span class="operator">(</span><span class="regex">?>...) Grab what we can, prohibit backtracking
(?</span><span class="operator">{</span> <span class="variable">code</span> <span class="operator">})</span> <span class="variable">Embedded</span> <span class="variable">code</span><span class="operator">,</span> <span class="keyword">return</span> <span class="variable">value</span> <span class="variable">becomes</span> <span class="variable">$^R</span>
<span class="operator">(</span><span class="regex">??</span><span class="operator">{</span> <span class="variable">code</span> <span class="operator">})</span> <span class="variable">Dynamic</span> <span class="variable">regex</span><span class="operator">,</span> <span class="keyword">return</span> <span class="variable">value</span> <span class="variable">used</span> <span class="variable">as</span> <span class="variable">regex</span>
<span class="operator">(</span><span class="regex">?(cond)yes|no) cond being integer corresponding to capturing parens
(?</span><span class="operator">(</span><span class="variable">cond</span><span class="operator">)</span><span class="variable">yes</span><span class="operator">)</span> <span class="keyword">or</span> <span class="variable">a</span> <span class="variable">lookaround</span><span class="operator">/</span><span class="keyword">eval</span> <span class="variable">zero</span><span class="operator">-</span><span class="variable">width</span> <span class="variable">assertion</span>
</pre>
<p>
</p>
<h2><a name="variables">VARIABLES</a></h2>
<pre>
<span class="variable">$_</span> <span class="variable">Default</span> <span class="variable">variable</span> <span class="keyword">for</span> <span class="variable">operators</span> <span class="variable">to</span> <span class="keyword">use</span>
<span class="variable">$*</span> <span class="variable">Enable</span> <span class="variable">multiline</span> <span class="variable">matching</span> <span class="operator">(</span><span class="variable">deprecated</span><span class="operator">;</span> <span class="keyword">not</span> <span class="variable">in</span> <span class="number">5.9</span><span class="operator">.</span><span class="number">0</span> <span class="keyword">or</span> <span class="variable">later</span><span class="operator">)</span>
</pre>
<pre>
$& Entire matched string
$` Everything prior to matched string
$' Everything after to matched string</pre>
<p>The use of those last three will slow down <strong>all</strong> regex use
within your program. Consult <a href="../../lib/Pod/perlvar.html">the perlvar manpage</a> for <a href="../../lib/Pod/perlvar.html#item__last_match_start"><code>@LAST_MATCH_START</code></a>
to see equivalent expressions that won't cause slow down.
See also <a href="../../Devel/SawAmpersand.html">the Devel::SawAmpersand manpage</a>.</p>
<pre>
<span class="variable">$1</span><span class="operator">,</span> <span class="variable">$2</span> <span class="operator">...</span> <span class="variable">hold</span> <span class="variable">the</span> <span class="variable">Xth</span> <span class="variable">captured</span> <span class="variable">expr</span>
<span class="variable">$+</span> <span class="variable">Last</span> <span class="variable">parenthesized</span> <span class="variable">pattern</span> <span class="variable">match</span>
<span class="variable">$^N</span> <span class="variable">Holds</span> <span class="variable">the</span> <span class="variable">most</span> <span class="variable">recently</span> <span class="variable">closed</span> <span class="variable">capture</span>
<span class="variable">$^R</span> <span class="variable">Holds</span> <span class="variable">the</span> <span class="variable">result</span> <span class="variable">of</span> <span class="variable">the</span> <span class="keyword">last</span> <span class="operator">(</span><span class="regex">?{...}) expr
@- Offsets of starts of groups. $-[0] holds start of whole match
@+ Offsets of ends of groups. $+[0] holds end of whole match
</span>
</pre>
<p>Captured groups are numbered according to their <em>opening</em> paren.</p>
<p>
</p>
<h2><a name="functions">FUNCTIONS</a></h2>
<pre>
lc Lowercase a string
lcfirst Lowercase first char of a string
uc Uppercase a string
ucfirst Titlecase first char of a string</pre>
<pre>
pos Return or set current match position
quotemeta Quote metacharacters
reset Reset ?pattern? status
study Analyze string for optimizing matching</pre>
<pre>
split Use regex to split a string into parts</pre>
<p>The first four of these are like the escape sequences <code>\L</code>, <code>\l</code>,
<code>\U</code>, and <code>\u</code>. For Titlecase, see <a href="#titlecase">Titlecase</a>.</p>
<p>
</p>
<h2><a name="terminology">TERMINOLOGY</a></h2>
<p>
</p>
<h3><a name="titlecase">Titlecase</a></h3>
<p>Unicode concept which most often is equal to uppercase, but for
certain characters like the German "sharp s" there is a difference.</p>
<p>
</p>
<hr />
<h1><a name="author">AUTHOR</a></h1>
<p>Iain Truskett.</p>
<p>This document may be distributed under the same terms as Perl itself.</p>
<p>
</p>
<hr />
<h1><a name="see_also">SEE ALSO</a></h1>
<ul>
<li>
<p><a href="../../lib/Pod/perlretut.html">the perlretut manpage</a> for a tutorial on regular expressions.</p>
</li>
<li>
<p><a href="../../lib/Pod/perlrequick.html">the perlrequick manpage</a> for a rapid tutorial.</p>
</li>
<li>
<p><a href="../../lib/Pod/perlre.html">the perlre manpage</a> for more details.</p>
</li>
<li>
<p><a href="../../lib/Pod/perlvar.html">the perlvar manpage</a> for details on the variables.</p>
</li>
<li>
<p><a href="../../lib/Pod/perlop.html">the perlop manpage</a> for details on the operators.</p>
</li>
<li>
<p><a href="../../lib/Pod/perlfunc.html">the perlfunc manpage</a> for details on the functions.</p>
</li>
<li>
<p><a href="../../lib/Pod/perlfaq6.html">the perlfaq6 manpage</a> for FAQs on regular expressions.</p>
</li>
<li>
<p>The <a href="../../lib/re.html">the re manpage</a> module to alter behaviour and aid
debugging.</p>
</li>
<li>
<p><a href="../../lib/Pod/perldebug.html#debugging_regular_expressions">Debugging regular expressions in the perldebug manpage</a></p>
</li>
<li>
<p><a href="../../lib/Pod/perluniintro.html">the perluniintro manpage</a>, <a href="../../lib/Pod/perlunicode.html">the perlunicode manpage</a>, <a href="../../lib/charnames.html">the charnames manpage</a> and <a href="../../lib/locale.html">the locale manpage</a>
for details on regexes and internationalisation.</p>
</li>
<li>
<p><em>Mastering Regular Expressions</em> by Jeffrey Friedl
(<em><a href="http://regex.info/">http://regex.info/</a></em>) for a thorough grounding and
reference on the topic.</p>
</li>
</ul>
<p>
</p>
<hr />
<h1><a name="thanks">THANKS</a></h1>
<p>David P.C. Wollmann,
Richard Soderberg,
Sean M. Burke,
Tom Christiansen,
Jim Cromie,
and
Jeffrey Goff
for useful advice.</p>
</body>
</html>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?