perlretut.html

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

HTML
919
字号
<?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>perlretut - Perl regular expressions tutorial</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>perlretut - Perl regular expressions tutorial</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>
	<li><a href="#part_1__the_basics">Part 1: The basics</a></li>
	<ul>

		<li><a href="#simple_word_matching">Simple word matching</a></li>
		<li><a href="#using_character_classes">Using character classes</a></li>
		<li><a href="#matching_this_or_that">Matching this or that</a></li>
		<li><a href="#grouping_things_and_hierarchical_matching">Grouping things and hierarchical matching</a></li>
		<li><a href="#extracting_matches">Extracting matches</a></li>
		<li><a href="#matching_repetitions">Matching repetitions</a></li>
		<li><a href="#building_a_regexp">Building a regexp</a></li>
		<li><a href="#using_regular_expressions_in_perl">Using regular expressions in Perl</a></li>
	</ul>

	<li><a href="#part_2__power_tools">Part 2: Power tools</a></li>
	<ul>

		<li><a href="#more_on_characters__strings__and_character_classes">More on characters, strings, and character classes</a></li>
		<li><a href="#compiling_and_saving_regular_expressions">Compiling and saving regular expressions</a></li>
		<li><a href="#embedding_comments_and_modifiers_in_a_regular_expression">Embedding comments and modifiers in a regular expression</a></li>
		<li><a href="#noncapturing_groupings">Non-capturing groupings</a></li>
		<li><a href="#looking_ahead_and_looking_behind">Looking ahead and looking behind</a></li>
		<li><a href="#using_independent_subexpressions_to_prevent_backtracking">Using independent subexpressions to prevent backtracking</a></li>
		<li><a href="#conditional_expressions">Conditional expressions</a></li>
		<li><a href="#a_bit_of_magic__executing_perl_code_in_a_regular_expression">A bit of magic: executing Perl code in a regular expression</a></li>
		<li><a href="#pragmas_and_debugging">Pragmas and debugging</a></li>
	</ul>

	<li><a href="#bugs">BUGS</a></li>
	<li><a href="#see_also">SEE ALSO</a></li>
	<li><a href="#author_and_copyright">AUTHOR AND COPYRIGHT</a></li>
	<ul>

		<li><a href="#acknowledgments">Acknowledgments</a></li>
	</ul>

</ul>
<!-- INDEX END -->

<hr />
<p>
</p>
<h1><a name="name">NAME</a></h1>
<p>perlretut - Perl regular expressions tutorial</p>
<p>
</p>
<hr />
<h1><a name="description">DESCRIPTION</a></h1>
<p>This page provides a basic tutorial on understanding, creating and
using regular expressions in Perl.  It serves as a complement to the
reference page on regular expressions <a href="../../lib/Pod/perlre.html">the perlre manpage</a>.  Regular expressions
are an integral part of the <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 <a href="../../lib/Pod/perlfunc.html#item_split"><code>split</code></a>
operators and so this tutorial also overlaps with
<a href="../../lib/Pod/perlop.html#regexp_quotelike_operators">Regexp Quote-Like Operators in the perlop manpage</a> and <a href="../../lib/Pod/perlfunc.html#split">split in the perlfunc manpage</a>.</p>
<p>Perl is widely renowned for excellence in text processing, and regular
expressions are one of the big factors behind this fame.  Perl regular
expressions display an efficiency and flexibility unknown in most
other computer languages.  Mastering even the basics of regular
expressions will allow you to manipulate text with surprising ease.</p>
<p>What is a regular expression?  A regular expression is simply a string
that describes a pattern.  Patterns are in common use these days;
examples are the patterns typed into a search engine to find web pages
and the patterns used to list files in a directory, e.g., <code>ls *.txt</code>
or <code>dir *.*</code>.  In Perl, the patterns described by regular expressions
are used to search strings, extract desired parts of strings, and to
do search and replace operations.</p>
<p>Regular expressions have the undeserved reputation of being abstract
and difficult to understand.  Regular expressions are constructed using
simple concepts like conditionals and loops and are no more difficult
to understand than the corresponding <code>if</code> conditionals and <code>while</code>
loops in the Perl language itself.  In fact, the main challenge in
learning regular expressions is just getting used to the terse
notation used to express these concepts.</p>
<p>This tutorial flattens the learning curve by discussing regular
expression concepts, along with their notation, one at a time and with
many examples.  The first part of the tutorial will progress from the
simplest word searches to the basic regular expression concepts.  If
you master the first part, you will have all the tools needed to solve
about 98% of your needs.  The second part of the tutorial is for those
comfortable with the basics and hungry for more power tools.  It
discusses the more advanced regular expression operators and
introduces the latest cutting edge innovations in 5.6.0.</p>
<p>A note: to save time, 'regular expression' is often abbreviated as
regexp or regex.  Regexp is a more natural abbreviation than regex, but
is harder to pronounce.  The Perl pod documentation is evenly split on
regexp vs regex; in Perl, there is more than one way to abbreviate it.
We'll use regexp in this tutorial.</p>
<p>
</p>
<hr />
<h1><a name="part_1__the_basics">Part 1: The basics</a></h1>
<p>
</p>
<h2><a name="simple_word_matching">Simple word matching</a></h2>
<p>The simplest regexp is simply a word, or more generally, a string of
characters.  A regexp consisting of a word matches any string that
contains that word:</p>
<pre>
    <span class="string">"Hello World"</span> <span class="operator">=~</span> <span class="regex">/World/</span><span class="operator">;</span>  <span class="comment"># matches</span>
</pre>
<p>What is this perl statement all about? <code>&quot;Hello World&quot;</code> is a simple
double quoted string.  <code>World</code> is the regular expression and the
<code>//</code> enclosing <code>/World/</code> tells perl to search a string for a match.
The operator <code>=~</code> associates the string with the regexp match and
produces a true value if the regexp matched, or false if the regexp
did not match.  In our case, <code>World</code> matches the second word in
<code>&quot;Hello World&quot;</code>, so the expression is true.  Expressions like this
are useful in conditionals:</p>
<pre>
    <span class="keyword">if</span> <span class="operator">(</span><span class="string">"Hello World"</span> <span class="operator">=~</span> <span class="regex">/World/</span><span class="operator">)</span> <span class="operator">{</span>
        <span class="keyword">print</span> <span class="string">"It matches\n"</span><span class="operator">;</span>
    <span class="operator">}</span>
    <span class="keyword">else</span> <span class="operator">{</span>
        <span class="keyword">print</span> <span class="string">"It doesn't match\n"</span><span class="operator">;</span>
    <span class="operator">}</span>
</pre>
<p>There are useful variations on this theme.  The sense of the match can
be reversed by using <code>!~</code> operator:</p>
<pre>
    <span class="keyword">if</span> <span class="operator">(</span><span class="string">"Hello World"</span> <span class="operator">!~</span> <span class="regex">/World/</span><span class="operator">)</span> <span class="operator">{</span>
        <span class="keyword">print</span> <span class="string">"It doesn't match\n"</span><span class="operator">;</span>
    <span class="operator">}</span>
    <span class="keyword">else</span> <span class="operator">{</span>
        <span class="keyword">print</span> <span class="string">"It matches\n"</span><span class="operator">;</span>
    <span class="operator">}</span>
</pre>
<p>The literal string in the regexp can be replaced by a variable:</p>
<pre>
    <span class="variable">$greeting</span> <span class="operator">=</span> <span class="string">"World"</span><span class="operator">;</span>
    <span class="keyword">if</span> <span class="operator">(</span><span class="string">"Hello World"</span> <span class="operator">=~</span> <span class="regex">/$greeting/</span><span class="operator">)</span> <span class="operator">{</span>
        <span class="keyword">print</span> <span class="string">"It matches\n"</span><span class="operator">;</span>
    <span class="operator">}</span>
    <span class="keyword">else</span> <span class="operator">{</span>
        <span class="keyword">print</span> <span class="string">"It doesn't match\n"</span><span class="operator">;</span>
    <span class="operator">}</span>
</pre>
<p>If you're matching against the special default variable <a href="../../lib/Pod/perlvar.html#item___"><code>$_</code></a>, the
<a href="../../lib/Pod/perlvar.html#item___"><code>$_ =~</code></a> part can be omitted:</p>
<pre>
    <span class="variable">$_</span> <span class="operator">=</span> <span class="string">"Hello World"</span><span class="operator">;</span>
    <span class="keyword">if</span> <span class="operator">(</span><span class="regex">/World/</span><span class="operator">)</span> <span class="operator">{</span>
        <span class="keyword">print</span> <span class="string">"It matches\n"</span><span class="operator">;</span>
    <span class="operator">}</span>
    <span class="keyword">else</span> <span class="operator">{</span>
        <span class="keyword">print</span> <span class="string">"It doesn't match\n"</span><span class="operator">;</span>
    <span class="operator">}</span>
</pre>
<p>And finally, the <code>//</code> default delimiters for a match can be changed
to arbitrary delimiters by putting an <code>'m'</code> out front:</p>
<pre>
    <span class="string">"Hello World"</span> <span class="operator">=~</span> <span class="regex">m!World!</span><span class="operator">;</span>   <span class="comment"># matches, delimited by '!'</span>
    <span class="string">"Hello World"</span> <span class="operator">=~</span> <span class="regex">m{World}</span><span class="operator">;</span>   <span class="comment"># matches, note the matching '{}'</span>
    <span class="string">"/usr/bin/perl"</span> <span class="operator">=~</span> <span class="regex">m"/perl"</span><span class="operator">;</span> <span class="comment"># matches after '/usr/bin',</span>
                                 <span class="comment"># '/' becomes an ordinary char</span>
</pre>
<p><code>/World/</code>, <code>m!World!</code>, and <a href="../../lib/Pod/perlguts.html#item_m"><code>m{World}</code></a> all represent the
same thing.  When, e.g., <code>&quot;&quot;</code> is used as a delimiter, the forward
slash <code>'/'</code> becomes an ordinary character and can be used in a regexp

⌨️ 快捷键说明

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