perlreref.html

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

HTML
366
字号
<?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>perlreref - Perl Regular Expressions Reference</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>perlreref - Perl Regular Expressions Reference</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="#operators">OPERATORS</a></li>
		<li><a href="#syntax">SYNTAX</a></li>
		<li><a href="#escape_sequences">ESCAPE SEQUENCES</a></li>
		<li><a href="#character_classes">CHARACTER CLASSES</a></li>
		<li><a href="#anchors">ANCHORS</a></li>
		<li><a href="#quantifiers">QUANTIFIERS</a></li>
		<li><a href="#extended_constructs">EXTENDED CONSTRUCTS</a></li>
		<li><a href="#variables">VARIABLES</a></li>
		<li><a href="#functions">FUNCTIONS</a></li>
		<li><a href="#terminology">TERMINOLOGY</a></li>
		<ul>

			<li><a href="#titlecase">Titlecase</a></li>
		</ul>

	</ul>

	<li><a href="#author">AUTHOR</a></li>
	<li><a href="#see_also">SEE ALSO</a></li>
	<li><a href="#thanks">THANKS</a></li>
</ul>
<!-- INDEX END -->

<hr />
<p>
</p>
<h1><a name="name">NAME</a></h1>
<p>perlreref - Perl Regular Expressions Reference</p>
<p>
</p>
<hr />
<h1><a name="description">DESCRIPTION</a></h1>
<p>This is a quick reference to Perl's regular expressions.
For full information see <a href="../../lib/Pod/perlre.html">the perlre manpage</a> and <a href="../../lib/Pod/perlop.html">the perlop manpage</a>, as well
as the <a href="#see_also">SEE ALSO</a> section in this document.</p>
<p>
</p>
<h2><a name="operators">OPERATORS</a></h2>
<pre>
  =~ determines to which variable the regex is applied.
     In its absence, $_ is used.</pre>
<pre>
        <span class="variable">$var</span> <span class="operator">=~</span> <span class="regex">/foo/</span><span class="operator">;</span>
</pre>
<pre>
  <span class="operator">!~</span> <span class="variable">determines</span> <span class="variable">to</span> <span class="variable">which</span> <span class="variable">variable</span> <span class="variable">the</span> <span class="variable">regex</span> <span class="variable">is</span> <span class="variable">applied</span><span class="operator">,</span>
     <span class="keyword">and</span> <span class="variable">negates</span> <span class="variable">the</span> <span class="variable">result</span> <span class="variable">of</span> <span class="variable">the</span> <span class="variable">match</span><span class="operator">;</span> <span class="variable">it</span> <span class="variable">returns</span>
     <span class="variable">false</span> <span class="keyword">if</span> <span class="variable">the</span> <span class="variable">match</span> <span class="variable">succeeds</span><span class="operator">,</span> <span class="keyword">and</span> <span class="variable">true</span> <span class="keyword">if</span> <span class="variable">it</span> <span class="variable">fails</span><span class="operator">.</span>
</pre>
<pre>
       <span class="variable">$var</span> <span class="operator">!~</span> <span class="regex">/foo/</span><span class="operator">;</span>
</pre>
<pre>
  m/pattern/igmsoxc searches a string for a pattern match,
     applying the given options.</pre>
<pre>
        i  case-Insensitive
        g  Global - all occurrences
        m  Multiline mode - ^ and $ match internal lines
        s  match as a Single line - . matches \n
        o  compile pattern Once
        x  eXtended legibility - free whitespace and comments
        c  don't reset pos on failed matches when using /g</pre>
<pre>
     If 'pattern' is an empty string, the last I&lt;successfully&gt; matched
     regex is used. Delimiters other than '/' may be used for both this
     operator and the following ones.</pre>
<pre>
  qr/pattern/imsox lets you store a regex in a variable,
     or pass one around. Modifiers as for m// and are stored
     within the regex.</pre>
<pre>
  s/pattern/replacement/igmsoxe substitutes matches of
     'pattern' with 'replacement'. Modifiers as for m//
     with one addition:</pre>
<pre>
        e  Evaluate replacement as an expression</pre>
<pre>
     'e' may be specified multiple times. 'replacement' is interpreted
     as a double quoted string unless a single-quote (') is the delimiter.</pre>
<pre>
  ?pattern? is like m/pattern/ but matches only once. No alternate
      delimiters can be used. Must be reset with L&lt;reset|<a href="../../lib/Pod/perlfunc.html">perlfunc</a>/reset&gt;.</pre>
<p>
</p>
<h2><a name="syntax">SYNTAX</a></h2>
<pre>
   \       Escapes the character immediately following it
   .       Matches any single character except a newline (unless /s is used)
   ^       Matches at the beginning of the string (or line, if /m is used)
   $       Matches at the end of the string (or line, if /m is used)
   *       Matches the preceding element 0 or more times
   +       Matches the preceding element 1 or more times
   ?       Matches the preceding element 0 or 1 times
   {...}   Specifies a range of occurrences for the element preceding it
   [...]   Matches any one of the characters contained within the brackets
   (...)   Groups subexpressions for capturing to $1, $2...
   (?:...) Groups subexpressions without capturing (cluster)
   |       Matches either the subexpression preceding or following it
   \1, \2 ...  The text from the Nth group</pre>
<p>
</p>
<h2><a name="escape_sequences">ESCAPE SEQUENCES</a></h2>
<p>These work as in normal strings.</p>
<pre>
   <span class="operator">\</span><span class="variable">a</span>       <span class="variable">Alarm</span> <span class="operator">(</span><span class="variable">beep</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">f</span>       <span class="variable">Formfeed</span>
   <span class="operator">\</span><span class="variable">n</span>       <span class="variable">Newline</span>
   <span class="operator">\</span><span class="variable">r</span>       <span class="variable">Carriage</span> <span class="keyword">return</span>
   <span class="operator">\</span><span class="variable">t</span>       <span class="variable">Tab</span>
   <span class="operator">\</span><span class="number">037</span>     <span class="variable">Any</span> <span class="variable">octal</span> <span class="variable">ASCII</span> <span class="variable">value</span>
   <span class="operator">\</span><span class="variable">x7f</span>     <span class="variable">Any</span> <span class="variable">hexadecimal</span> <span class="variable">ASCII</span> <span class="variable">value</span>
   <span class="operator">\x</span><span class="operator">{</span><span class="string">263a</span><span class="operator">}</span> <span class="variable">A</span> <span class="variable">wide</span> <span class="variable">hexadecimal</span> <span class="variable">value</span>
   <span class="operator">\</span><span class="variable">cx</span>      <span class="variable">Control</span><span class="keyword">-x</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">A</span> <span class="variable">named</span> <span class="variable">character</span>
</pre>
<pre>
   \l  Lowercase next character
   \u  Titlecase next character
   \L  Lowercase until \E
   \U  Uppercase until \E
   \Q  Disable pattern metacharacters until \E
   \E  End case modification</pre>
<p>For Titlecase, see <a href="#titlecase">Titlecase</a>.</p>
<p>This one works differently from normal strings:</p>
<pre>
   \b  An assertion, not backspace, except in a character class</pre>
<p>
</p>
<h2><a name="character_classes">CHARACTER CLASSES</a></h2>
<pre>
   [amy]    Match 'a', 'm' or 'y'
   [f-j]    Dash specifies &quot;range&quot;
   [f-j-]   Dash escaped or at start or end means 'dash'
   [^f-j]   Caret indicates &quot;match any character _except_ these&quot;</pre>
<p>The following sequences work within or without a character class.
The first six are locale aware, all are Unicode aware.  The default
character class equivalent are given.  See <a href="../../lib/Pod/perllocale.html">the perllocale manpage</a> and
<a href="../../lib/Pod/perlunicode.html">the perlunicode manpage</a> for details.</p>
<pre>
   \d      A digit                     [0-9]
   \D      A nondigit                  [^0-9]
   \w      A word character            [a-zA-Z0-9_]
   \W      A non-word character        [^a-zA-Z0-9_]
   \s      A whitespace character      [ \t\n\r\f]
   \S      A non-whitespace character  [^ \t\n\r\f]</pre>
<pre>
   <span class="operator">\</span><span class="variable">C</span>      <span class="variable">Match</span> <span class="variable">a</span> <span class="variable">byte</span> <span class="operator">(</span><span class="variable">with</span> <span class="variable">Unicode</span><span class="operator">,</span> <span class="string">'.'</span> <span class="variable">matches</span> <span class="variable">a</span> <span class="variable">character</span><span class="operator">)</span>
   <span class="operator">\</span><span class="variable">pP</span>     <span class="variable">Match</span> <span class="variable">P</span><span class="operator">-</span><span class="variable">named</span> <span class="operator">(</span><span class="variable">Unicode</span><span class="operator">)</span> <span class="variable">property</span>
   <span class="operator">\</span><span class="variable">p</span><span class="operator">{...}</span> <span class="variable">Match</span> <span class="variable">Unicode</span> <span class="variable">property</span> <span class="variable">with</span> <span class="variable">long</span> <span class="variable">name</span>
   <span class="operator">\</span><span class="variable">PP</span>     <span class="variable">Match</span> <span class="variable">non</span><span class="operator">-</span><span class="variable">P</span>
   <span class="operator">\</span><span class="variable">P</span><span class="operator">{...}</span> <span class="variable">Match</span> <span class="variable">lack</span> <span class="variable">of</span> <span class="variable">Unicode</span> <span class="variable">property</span> <span class="variable">with</span> <span class="variable">long</span> <span class="variable">name</span>
   <span class="operator">\</span><span class="variable">X</span>      <span class="variable">Match</span> <span class="variable">extended</span> <span class="variable">unicode</span> <span class="variable">sequence</span>

⌨️ 快捷键说明

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