⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch04_06.htm

📁 by Randal L. Schwartz and Tom Phoenix ISBN 0-596-00132-0 Third Edition, published July 2001. (See
💻 HTM
📖 第 1 页 / 共 3 页
字号:
<html><head><title>Regular Expressions (Perl in a Nutshell, 2nd Edition)</title><link rel="stylesheet" type="text/css" href="../style/style1.css" /><meta name="DC.Creator" content="Stephen Spainhour" /><meta name="DC.Format" content="text/xml" scheme="MIME" /><meta name="DC.Language" content="en-US" /><meta name="DC.Publisher" content="O'Reilly &amp; Associates, Inc." /><meta name="DC.Source" scheme="ISBN" content="0596002416L" /><meta name="DC.Subject.Keyword" content="stuff" /><meta name="DC.Title" content="Perl in a Nutshell, 2nd Edition" /><meta name="DC.Type" content="Text.Monograph" /></head><body bgcolor="#ffffff"><img src="gifs/smbanner.gif" usemap="#banner-map" border="0" alt="Book Home" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Java and XSLT" /><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch04_05.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0" /></a></td><td align="center" valign="top" width="228" /><td align="right" valign="top" width="228"><a href="ch04_07.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr></table></div><h2 class="sect1">4.6. Regular Expressions</h2><a name="INDEX-595" /><p><a name="INDEX-596" />Regular expressions are used severalways in Perl. They're used in conditionals todetermine whether a string matches a particular pattern.They're also used to find patterns in strings andreplace the match with something else.</p><p><a name="INDEX-597" /><a name="INDEX-598" />Theordinary pattern match operator looks like<tt class="literal">/</tt><em class="replaceable"><tt>pattern</tt></em><tt class="literal">/</tt>.It matches against the <tt class="literal">$_</tt> variable by default. Ifthe pattern is found in the string, the operator returns true(<tt class="literal">1</tt>); if there is no match, a false value("") is returned.</p><p><a name="INDEX-599" /><a name="INDEX-600" />The substitution operator looks like<tt class="literal">s/</tt><em class="replaceable"><tt>pattern</em><tt class="literal">/</tt><em class="replaceable">replace</tt></em><tt class="literal">/</tt>.This operator searches <tt class="literal">$_</tt> by default. If it findsthe specified <em class="replaceable"><tt>pattern</tt></em>, it is replaced withthe string in <em class="replaceable"><tt>replace</tt></em>. If<em class="replaceable"><tt>pattern</tt></em> is not matched, nothing happens.</p><p>You may specify a variable other than <tt class="literal">$_</tt> with the<tt class="literal">=~</tt> binding operator (or the negated<tt class="literal">!~</tt> binding operator, which returns true if thepattern is not matched). For example:</p><blockquote><pre class="code">$text =~ /sampo/;</pre></blockquote><a name="perlnut2-CHP-4-SECT-6.1" /><div class="sect2"><h3 class="sect2">4.6.1. Pattern-Matching Operators</h3><p><a name="INDEX-601" /><a name="INDEX-602" />The following list definesPerl's pattern-matching operators. Some of theoperators have alternative"quoting" schemes and have a set ofmodifiers that can be placed directly after the operators to affectthe match operation in some way.</p><dl><dt><b><tt class="literal">m/</tt><em class="replaceable">pattern</em><tt class="literal">/gimosxe</tt></b></dt><dd>Searches a string for a pattern match. Modifiersare<a name="INDEX-603" /><a name="INDEX-604" /><a name="INDEX-605" /><a name="INDEX-606" /><a name="INDEX-607" /><a name="INDEX-608" />:</p><a name="ch04-6-fm2xml" /><table border="1" cellpadding="3"><tr><th><p>Modifier</p></th><th><p>Meaning</p></th></tr><tr><td><p><tt class="literal">g</tt></p></td><td><p>Match globally, i.e., find all occurrences.</p></td></tr><tr><td><p><tt class="literal">i</tt></p></td><td><p>Do case-insensitive pattern matching.</p></td></tr><tr><td><p><tt class="literal">m</tt></p></td><td><p>Treat string as multiple lines.</p></td></tr><tr><td><p><tt class="literal">o</tt></p></td><td><p>Compile pattern only once.</p></td></tr><tr><td><p><tt class="literal">s</tt></p></td><td><p>Treat string as single line.</p></td></tr><tr><td><p><tt class="literal">x</tt></p></td><td><p>Use extended regular expressions.</p></td></tr></table><p><p>If <tt class="literal">/</tt> is the delimiter, then the initial<tt class="literal">m</tt> is optional. With <tt class="literal">m</tt>, you canuse any pair of non-alphanumeric, non-whitespace characters asdelimiters.</p></dd><dt><b><tt class="literal">?</tt><em class="replaceable">pattern</em><tt class="literal">?</tt></b></dt><dd><a name="INDEX-609" /><a name="INDEX-610" />This operator is just like the<tt class="literal">m/</tt><em class="replaceable"><tt>pattern</tt></em><tt class="literal">/</tt>search, except it matches only once.</p></dd><dt><b><tt class="literal">qr/</tt><em class="replaceable">pattern</em><tt class="literal">/imosx</tt></b></dt><dd><a name="INDEX-611" />Creates a precompiled regularexpression from <em class="replaceable"><tt>pattern</tt></em>, which can bepassed around in variables and interpolated into other regularexpressions. The modifiers are the same as those for<tt class="literal">m//</tt> above.</p></dd><dt><b><tt class="literal">s/</tt><em class="replaceable">pattern</em><tt class="literal">/</tt><em class="replaceable">replacement</em><tt class="literal">/egimosx</tt></b></dt><dd><a name="INDEX-612" /><a name="INDEX-613" />Searches a string for<em class="replaceable"><tt>pattern</tt></em> and replaces any match with the<em class="replaceable"><tt>replacement</tt></em> text. Returns the number ofsubstitutions made, which can be more than one with the<tt class="literal">/g</tt> modifier. Otherwise, it returns false(<tt class="literal">0</tt>). If no string is specified via the<tt class="literal">=~</tt> or <tt class="literal">!~</tt> operator, the<tt class="literal">$_</tt> variable is searched and modified. Modifiersare:</p><a name="ch04-7-fm2xml" /><table border="1" cellpadding="3"><tr><th><p>Modifier</p></th><th><p>Meaning</p></th></tr><tr><td><p><tt class="literal">e</tt></p></td><td><p>Evaluate the right side as an expression.</p></td></tr><tr><td><p><tt class="literal">g</tt></p></td><td><p>Replace globally, i.e., all occurrences.</p></td></tr><tr><td><p><tt class="literal">cg</tt></p></td><td><p>Continue search after <tt class="literal">g</tt> failed. No longersupported for <tt class="literal">s///</tt> as of Perl 5.8.</p></td></tr><tr><td><p><tt class="literal">i</tt></p></td><td><p>Do case-insensitive pattern matching.</p></td></tr><tr><td><p><tt class="literal">m</tt></p></td><td><p>Treat string as multiple lines.</p></td></tr><tr><td><p><tt class="literal">o</tt></p></td><td><p>Compile pattern only once.</p></td></tr><tr><td><p><tt class="literal">s</tt></p></td><td><p>Treat string as single line.</p></td></tr><tr><td><p><tt class="literal">x</tt></p></td><td><p>Use extended regular expressions.</p></td></tr></table><p><p>Any non-alphanumeric, non-whitespace delimiter may replace theslashes. If single quotes are used, no interpretation is done on thereplacement string (the <tt class="literal">/e</tt> modifier overridesthis, however).</p></dd><dt><b><tt class="literal">tr/</tt><em class="replaceable">pattern1</em><tt class="literal">/</tt><em class="replaceable">pattern2</em><tt class="literal">/cds</tt></b></dt><dt><b><tt class="literal">y/</tt><em class="replaceable">pattern1</em><tt class="literal">/</tt><em class="replaceable">pattern2</em><tt class="literal">/cds</tt></b></dt><dd><a name="INDEX-614" /><a name="INDEX-615" />This operator scans a string characterby character and replaces all occurrences of the characters found in<em class="replaceable"><tt>pattern1</tt></em> with the corresponding characterin <em class="replaceable"><tt>pattern2</tt></em>. It returns the number ofcharacters replaced or deleted. If no string is specified via the<tt class="literal">=~</tt> or <tt class="literal">!~</tt> operator, the<tt class="literal">$_</tt> string is translated. Modifiers are:</p><a name="ch04-8-fm2xml" /><table border="1" cellpadding="3"><tr><th><p>Modifier</p></th><th><p>Meaning</p></th></tr><tr><td><p><tt class="literal">c</tt></p></td><td><p>Complement <em class="replaceable"><tt>pattern1</tt></em>.</p></td></tr><tr><td><p><tt class="literal">d</tt></p></td><td><p>Delete found but unreplaced characters.</p></td></tr><tr><td><p><tt class="literal">s</tt></p></td><td><p>Squash duplicate replaced characters.</p></td></tr></table><p></dd></dl></div><a name="perlnut2-CHP-4-SECT-6.2" /><div class="sect2"><h3 class="sect2">4.6.2. Regular Expression Syntax</h3><p><a name="INDEX-616" /><a name="INDEX-617" /><a name="INDEX-618" />The simplest kind ofregular expression is a literal string. More complicated patternsinvolve the use of <em class="emphasis">metacharacters</em> to describeall the different choices and variations that you want to build intoa pattern. Metacharacters don't match themselves,but describe something else. The metacharacters are:</p><a name="ch04-9-fm2xml" /><table border="1" cellpadding="3"><tr><th><p>Metacharacter</p></th><th><p>Meaning</p></th></tr><tr><td><p><tt class="literal">\</tt></p></td><td><p>Escapes the character(s) immediately following it</p></td></tr><tr><td><p><tt class="literal">.</tt></p></td><td><p>Matches any single character except a newline (unless<tt class="literal">/s</tt> is used)</p></td></tr><tr><td><p><tt class="literal">^</tt></p></td><td><p>Matches at the beginning of the string (or line, if<tt class="literal">/m</tt> is used)</p></td></tr><tr><td><p><tt class="literal">$</tt></p></td><td><p>Matches at the end of the string (or line, if <tt class="literal">/m</tt>is used)</p></td></tr><tr><td><p><tt class="literal">*</tt></p></td><td><p>Matches the preceding element 0 or more times</p></td></tr><tr><td><p><tt class="literal">+</tt></p></td><td><p>Matches the preceding element 1 or more times</p></td></tr><tr><td><p><tt class="literal">?</tt></p></td><td><p>Matches the preceding element 0 or 1 times</p></td></tr><tr><td><p><tt class="literal">{...}</tt></p></td><td><p>Specifies a range of occurrences for the element preceding it</p></td></tr><tr><td><p><tt class="literal">[...]</tt></p></td><td><p>Matches any one of the class of characters contained within thebrackets</p></td></tr><tr><td><p><tt class="literal">(...)</tt></p></td><td><p>Groups regular expressions</p></td></tr><tr><td><p><tt class="literal">|</tt></p></td><td><p>Matches either the expression preceding or following it</p></td></tr>

⌨️ 快捷键说明

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