📄 ch09_02.htm
字号:
<html><head><title>Option Modifiers (Learning Perl, 3rd Edition)</title><link rel="stylesheet" type="text/css" href="../style/style1.css" /><meta name="DC.Creator" content="Randal L. Schwartz and Tom Phoenix" /><meta name="DC.Format" content="text/xml" scheme="MIME" /><meta name="DC.Language" content="en-US" /><meta name="DC.Publisher" content="O'Reilly & Associates, Inc." /><meta name="DC.Source" scheme="ISBN" content="0596001320L" /><meta name="DC.Subject.Keyword" content="stuff" /><meta name="DC.Title" content="Learning Perl, 3rd Edition" /><meta name="DC.Type" content="Text.Monograph" /></head><body bgcolor="#ffffff"><img alt="Book Home" border="0" src="gifs/smbanner.gif" usemap="#banner-map" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Learning Perl, 3rd Edition" /><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="ch09_01.htm"><img alt="Previous" border="0" src="../gifs/txtpreva.gif" /></a></td><td align="center" valign="top" width="228"><a href="index.htm"></a></td><td align="right" valign="top" width="228"><a href="ch09_03.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr></table></div><h2 class="sect1">9.2. Option Modifiers</h2><p>There are several <a name="INDEX-581" />option modifier letters, sometimescalled<em class="firstterm">flags</em><a name="INDEX-582" />,which may be appended as a group right after the ending delimiter ofa regular expression to change its behavior from the default.</p><a name="lperl3-CHP-9-SECT-2.1" /><div class="sect2"><h3 class="sect2">9.2.1. Case-insensitive Matching with /i</h3><p>To make a case-insensitive pattern match, so that you can match<tt class="literal">FRED</tt> as easily as <tt class="literal">fred</tt> or<tt class="literal">Fred</tt>, use the<tt class="literal">/i</tt><a name="INDEX-583" /> <a name="INDEX-584" /><a name="INDEX-585" /> modifier:</p><blockquote><pre class="code">print "Would you like to play a game? ";chomp($_ = <STDIN>);if (/\byes\b/i) { # case-insensitive match print "In that case, I recommend that you go bowling.\n";}</pre></blockquote></div><a name="lperl3-CHP-9-SECT-2.2" /><div class="sect2"><h3 class="sect2">9.2.2. Matching Any Character with /s</h3><p>Do you ever feel frustrated that the <a name="INDEX-586" />dot (<tt class="literal">.</tt>) won'tmatch newline? If you might have newlines in your strings, and youwant the dot to be able to match them, the<tt class="literal">/s</tt><a name="INDEX-587" /><a name="INDEX-588" /><a name="INDEX-589" /> modifier will do the job. It changesevery dot<a href="#FOOTNOTE-194">[194]</a> in the pattern toact like the character class <tt class="literal">[\d\D]</tt> does, which isto match any character, even if it is a newline. Of course, you haveto have a string with newlines for this to make a difference:</p><blockquote class="footnote"> <a name="FOOTNOTE-194" /><p>[194]If you wish to change just some of them,and not all, you'll probably want to replace just those fewwith <tt class="literal">[\d\D]</tt>.</p> </blockquote><blockquote><pre class="code">$_ = "I saw Barney\ndown at the bowling alley\nwith Fred\nlast night.\n";if (/\bBarney\b.*\bFred\b/s) { print "That string mentions Fred after Barney!\n";}</pre></blockquote><p>Without the <tt class="literal">/s</tt> modifier, that match would fail,since the two names aren't on the same line.</p></div><a name="lperl3-CHP-9-SECT-2.3" /><div class="sect2"><h3 class="sect2">9.2.3. Combining Option Modifiers</h3><p>If you have more than one option modifier to use on the same pattern,they may be used one after the other; their order isn'tsignificant:</p><blockquote><pre class="code">if (/\bbarney\b.*\bfred\b/si) { # both /s and /i print "That string mentions Fred after Barney!\n";}</pre></blockquote></div><a name="lperl3-CHP-9-SECT-2.4" /><div class="sect2"><h3 class="sect2">9.2.4. Other Options</h3><p>There are many other option modifiers available. We'll coverthose as we get to them, or you can read about them in the<em class="emphasis">perlop</em> manpage and in the descriptions of<tt class="literal">m//</tt> and the other regular expression operatorsthat we'll see later in this chapter.</p></div><hr width="684" align="left" /><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch09_01.htm"><img alt="Previous" border="0" src="../gifs/txtpreva.gif" /></a></td><td align="center" valign="top" width="228"><a href="index.htm"><img alt="Home" border="0" src="../gifs/txthome.gif" /></a></td><td align="right" valign="top" width="228"><a href="ch09_03.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr><tr><td align="left" valign="top" width="228">9. Using Regular Expressions</td><td align="center" valign="top" width="228"><a href="index/index.htm"><img alt="Book Index" border="0" src="../gifs/index.gif" /></a></td><td align="right" valign="top" width="228">9.3. The Binding Operator, =~</td></tr></table></div><hr width="684" align="left" /><img alt="Library Navigation Links" border="0" src="../gifs/navbar.gif" usemap="#library-map" /><p><p><font size="-1"><a href="copyrght.htm">Copyright © 2002</a> O'Reilly & Associates. All rights reserved.</font></p><map name="library-map"><area shape="rect" coords="1,0,85,94" href="../index.htm"><area shape="rect" coords="86,1,178,103" href="../lwp/index.htm"><area shape="rect" coords="180,0,265,103" href="../lperl/index.htm"><area shape="rect" coords="267,0,353,105" href="../perlnut/index.htm"><area shape="rect" coords="354,1,446,115" href="../prog/index.htm"><area shape="rect" coords="448,0,526,132" href="../tk/index.htm"><area shape="rect" coords="528,1,615,119" href="../cookbook/index.htm"><area shape="rect" coords="617,0,690,135" href="../pxml/index.htm"></map></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -