re.html

来自「perl教程」· HTML 代码 · 共 110 行

HTML
110
字号
<?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>re - Perl pragma to alter regular expression behaviour</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__',1);</script>
<h1><a>re - Perl pragma to alter regular expression behaviour</a></h1>
<p><a name="__index__"></a></p>

<!-- INDEX BEGIN -->

<ul>

	<li><a href="#name">NAME</a></li>
	<li><a href="#synopsis">SYNOPSIS</a></li>
	<li><a href="#description">DESCRIPTION</a></li>
</ul>
<!-- INDEX END -->

<hr />
<p>
</p>
<h1><a name="name">NAME</a></h1>
<p>re - Perl pragma to alter regular expression behaviour</p>
<p>
</p>
<hr />
<h1><a name="synopsis">SYNOPSIS</a></h1>
<pre>
    <span class="keyword">use</span> <span class="variable">re</span> <span class="string">'taint'</span><span class="operator">;</span>
    <span class="operator">(</span><span class="variable">$x</span><span class="operator">)</span> <span class="operator">=</span> <span class="operator">(</span><span class="variable">$^X</span> <span class="operator">=~</span> <span class="regex">/^(.*)$/s</span><span class="operator">);</span>     <span class="comment"># $x is tainted here</span>
</pre>
<pre>
    <span class="variable">$pat</span> <span class="operator">=</span> <span class="string">'(?{ $foo = 1 })'</span><span class="operator">;</span>
    <span class="keyword">use</span> <span class="variable">re</span> <span class="string">'eval'</span><span class="operator">;</span>
    <span class="regex">/foo${pat}bar/</span><span class="operator">;</span>                <span class="comment"># won't fail (when not under -T switch)</span>
</pre>
<pre>
    <span class="operator">{</span>
        <span class="keyword">no</span> <span class="variable">re</span> <span class="string">'taint'</span><span class="operator">;</span>             <span class="comment"># the default</span>
        <span class="operator">(</span><span class="variable">$x</span><span class="operator">)</span> <span class="operator">=</span> <span class="operator">(</span><span class="variable">$^X</span> <span class="operator">=~</span> <span class="regex">/^(.*)$/s</span><span class="operator">);</span> <span class="comment"># $x is not tainted here</span>
</pre>
<pre>
        <span class="keyword">no</span> <span class="variable">re</span> <span class="string">'eval'</span><span class="operator">;</span>              <span class="comment"># the default</span>
        <span class="regex">/foo${pat}bar/</span><span class="operator">;</span>            <span class="comment"># disallowed (with or without -T switch)</span>
            <span class="operator">}</span>
</pre>
<pre>
    <span class="keyword">use</span> <span class="variable">re</span> <span class="string">'debug'</span><span class="operator">;</span>                <span class="comment"># NOT lexically scoped (as others are)</span>
    <span class="regex">/^(.*)$/s</span><span class="operator">;</span>                     <span class="comment"># output debugging info during</span>
                                   <span class="comment">#     compile and run time</span>
</pre>
<pre>
    <span class="keyword">use</span> <span class="variable">re</span> <span class="string">'debugcolor'</span><span class="operator">;</span>           <span class="comment"># same as 'debug', but with colored output</span>
    <span class="operator">...</span>
</pre>
<p>(We use $^X in these examples because it's tainted by default.)</p>
<p>
</p>
<hr />
<h1><a name="description">DESCRIPTION</a></h1>
<p>When <code>use re 'taint'</code> is in effect, and a tainted string is the target
of a regex, the regex memories (or values returned by the m// operator
in list context) are tainted.  This feature is useful when regex operations
on tainted data aren't meant to extract safe substrings, but to perform
other transformations.</p>
<p>When <code>use re 'eval'</code> is in effect, a regex is allowed to contain
<code>(?{ ... })</code> zero-width assertions even if regular expression contains
variable interpolation.  That is normally disallowed, since it is a
potential security risk.  Note that this pragma is ignored when the regular
expression is obtained from tainted data, i.e.  evaluation is always
disallowed with tainted regular expressions.  See <a href="../lib/Pod/perlre.html#___code___">(?{ code }) in the perlre manpage</a>.</p>
<p>For the purpose of this pragma, interpolation of precompiled regular
expressions (i.e., the result of <a href="../lib/Pod/perlfunc.html#item_qr_"><code>qr//</code></a>) is <em>not</em> considered variable
interpolation.  Thus:</p>
<pre>
    <span class="regex">/foo${pat}bar/</span>
</pre>
<p><em>is</em> allowed if $pat is a precompiled regular expression, even
if $pat contains <code>(?{ ... })</code> assertions.</p>
<p>When <code>use re 'debug'</code> is in effect, perl emits debugging messages when
compiling and using regular expressions.  The output is the same as that
obtained by running a <code>-DDEBUGGING</code>-enabled perl interpreter with the
<strong>-Dr</strong> switch. It may be quite voluminous depending on the complexity
of the match.  Using <code>debugcolor</code> instead of <code>debug</code> enables a
form of output that can be used to get a colorful display on terminals
that understand termcap color sequences.  Set <code>$ENV{PERL_RE_TC}</code> to a
comma-separated list of <code>termcap</code> properties to use for highlighting
strings on/off, pre-point part on/off.
See <a href="../lib/Pod/perldebug.html#debugging_regular_expressions">Debugging regular expressions in the perldebug manpage</a> for additional info.</p>
<p>The directive <code>use re 'debug'</code> is <em>not lexically scoped</em>, as the
other directives are.  It has both compile-time and run-time effects.</p>
<p>See <a href="../lib/Pod/perlmodlib.html#pragmatic_modules">Pragmatic Modules in the perlmodlib manpage</a>.</p>

</body>

</html>

⌨️ 快捷键说明

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