📄 ch10.htm
字号:
</TABLE></CENTER><P><P>All options are specified after the last pattern delimiter. ForinstaNCe, if you want the match to ignore the case of the charactersin the string, you can do this:<BLOCKQUOTE><PRE>$_ = "AAA BBB AAA";print "Found bbb\n" if m/bbb/i;</PRE></BLOCKQUOTE><P>This program finds a match even though the pattern uses lowercaseand the string uses uppercase because the <TT>/i</TT>option was used, telling Perl to ignore the case.<P>The result from a global pattern match can be assigned to an arrayvariable or used inside a loop. This feature comes in handy afteryou learn about meta-characters in the section called "Howto Create Patterns" later in this chapter.<P>For more information about the matching options, see the section,"Pattern Examples" later in this chapter.<H2><A NAME="TheSubstitutionOperators"><FONT SIZE=5 COLOR=#FF0000>The Substitution Operator (s///)</FONT></A></H2><P>The substitution operator (<TT>s///</TT>)is used to change strings. It requires two operands, like this:<BLOCKQUOTE><PRE>s/a/z/;</PRE></BLOCKQUOTE><P>This statement changes the first <TT>a</TT>in <TT>$_</TT> into a <TT>z</TT>.Not too complicated, huh? Things won't get complicated until westart talking about regular expressions in earnest in the section,"How to Create Patterns?" later in the chapter.<P>You can use variable interpolation with the substitution operatorjust as you can with the matching operator. For instaNCe:<BLOCKQUOTE><PRE>$needToReplace = "bbb";$replacementText = "1234567890";$_ = "AAA bbb AAA";$result = s/$needToReplace/$replacementText/;<BR></PRE></BLOCKQUOTE><p><CENTER><TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%><TR><TD><B>Note</B></TD></TR><TR><TD><BLOCKQUOTE>You can use variable interpolation in the replacement pattern as shown here, but none of the meta-characters described later in the chapter can be used in the replacement pattern.</BLOCKQUOTE></TD></TR></TABLE></CENTER><P><P>This program changes the <TT>$_</TT>variable to hold <TT>"AAA 1234567890AAA"</TT> instead of its original value, and the <TT>$result</TT>variable will be equal to <TT>1-</TT>thenumber of substitutions made.<P>Frequently, the substitution operator is used to remove substrings.For instaNCe, if you want to remove the <TT>"bbb"</TT>sequeNCe of characters from the <TT>$_</TT>variable, you could do this:<BLOCKQUOTE><PRE>s/bbb//;</PRE></BLOCKQUOTE><P>By replacing the matched string with nothing, you have effectivelydeleted it.<P>If brackets of any type are used as delimiters for the searchpattern, you need to use a second set of brackets to eNClose thereplacement pattern. For instaNCe:<BLOCKQUOTE><PRE>$_ = "AAA bbb AAA";$result = s{bbb}{1234567890};</PRE></BLOCKQUOTE><H3><A NAME="TheSubstitutionOptions">The Substitution Options</A></H3><P>Like the matching operator, the substitution operator has severaloptions. One interesting option is the capability to evaluatethe replacement pattern as an expression instead of a string.You could use this capability to find all numbers in a file andmultiply them by a given percentage, for instaNCe. Or, you couldrepeat matched strings by using the string repetition operator.Table 10.3 shows all of the options you can use with the substitutionoperator.<BR><P><CENTER><B>Table 10.3 Options for the SubstitutionOperator</B></CENTER><p><CENTER><TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%><TR><TD WIDTH=115><CENTER><I>Option</I></CENTER></TD><TD WIDTH=475><I>Description</I></TD></TR><TR><TD WIDTH=115><CENTER>e</CENTER></TD><TD WIDTH=475>This option forces Perl to evaluate the replacement pattern as an expression.</TD></TR><TR><TD WIDTH=115><CENTER>g</CENTER></TD><TD WIDTH=475>This option replaces all occurreNCes of the pattern in the string.</TD></TR><TR><TD WIDTH=115><CENTER>i</CENTER></TD><TD WIDTH=475>This option ignores the case of characters in the string.</TD></TR><TR><TD WIDTH=115><CENTER>m</CENTER></TD><TD WIDTH=475>This option treats the string as multiple lines. Perl does some optimization by assuming that <TT>$_</TT> contains a single line of input. If you know that it contains multiple newline characters, use this option to turn off the optimization.</TD></TR><TR><TD WIDTH=115><CENTER>o</CENTER></TD><TD WIDTH=475>This option compiles the pattern only oNCe. You can achieve some small performaNCe gains with this option. It should be used with variable interpolation only when the value of the variable will not change during the lifetime of the program.</TD></TR><TR><TD WIDTH=115><CENTER>s</CENTER></TD><TD WIDTH=475>This option treats the string as a single line.</TD></TR><TR><TD WIDTH=115><CENTER>x</CENTER></TD><TD WIDTH=475>This option lets you use extended regular expressions. Basically, this means that Perl ignores white space that is not escaped with a backslash or within a character class. I highly recommend this option so you can use spaces to make your regular expressions more readable. See the section, "Example: Extension Syntax," later in this chapter for more information.</TD></TR></TABLE></CENTER><P><P>The <TT>/e</TT> option changes theinterpretation of the pattern delimiters. If used, variable interpolationis active even if single quotes are used. In addition, if backquotes are used as delimiters, the replacement pattern is executedas a DOS or UNIX command. The output of the command then is usedas the replacement text.<H2><A NAME="TheTranslationOperatortr"><FONT SIZE=5 COLOR=#FF0000>The Translation Operator (tr///)</FONT></A></H2><P>The translation operator (<TT>tr///</TT>)is used to change individual characters in the <TT>$_</TT>variable. It requires two operands, like this:<BLOCKQUOTE><PRE>tr/a/z/;</PRE></BLOCKQUOTE><P>This statement translates all occurreNCes of <TT>a</TT>into <TT>z</TT>. If you specify morethan one character in the match character list, you can translatemultiple characters at a time.<P>For instaNCe:<BLOCKQUOTE><PRE>tr/ab/z/;</PRE></BLOCKQUOTE><P>translates all <TT>a</TT> and all<TT>b</TT> characters into the <TT>z</TT>character. If the replacement list of characters is shorter thanthe target list of characters, the last character in the replacementlist is repeated as often as needed. However, if more than onereplacement character is given for a matched character, only thefirst is used. For instaNCe:<BLOCKQUOTE><PRE>tr/WWW/ABC/;</PRE></BLOCKQUOTE><P>results in all <TT>W</TT> charactersbeing converted to an <TT>A</TT> character.The rest of the replacement list is ignored.<P>Unlike the matching and substitution operators, the translationoperator doesn't perform variable interpolation.<BR><p><CENTER><TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%><TR><TD><B>Note</B></TD></TR><TR><TD><BLOCKQUOTE>The <TT>tr</TT> operator gets its name from the UNIX <TT>tr</TT> utility. If you are familiar with the <TT>tr</TT> utility, then you already know how to use the <TT>tr</TT> operator.</BLOCKQUOTE><BLOCKQUOTE>The UNIX <TT>sed</TT> utility uses a <TT>y</TT> to indicate translations. To make learning Perl easier for <TT>sed</TT> users, <TT>y</TT> is supported as a synonym for <TT>tr</TT>.</BLOCKQUOTE></TD></TR></TABLE></CENTER><P><H3><A NAME="TheTranslationOptions">The Translation Options</A></H3><P>The translation operator has options different from the matchingand substitution operators. You can delete matched characters,replace repeated characters with a single character, and translateonly characters that don't match the character list. Table 10.4shows the translation options.<BR><P><CENTER><B>Table 10.4 Options for the Translation Operator</B></CENTER><p><CENTER><TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%><TR><TD WIDTH=79><CENTER><I>Option</I></CENTER></TD><TD WIDTH=511><I>Description</I></TD></TR><TR><TD WIDTH=79><CENTER>c</CENTER></TD><TD WIDTH=511>This option complements the match character list. In other words, the translation is done for every character that does not match the character list.</TD></TR><TR><TD WIDTH=79><CENTER>d</CENTER></TD><TD WIDTH=511>This option deletes any character in the match list that does not have a corresponding character in the replacement list.</TD></TR><TR><TD WIDTH=79><CENTER>s</CENTER></TD><TD WIDTH=511>This option reduces repeated instaNCes of matched characters to a single instaNCe of that character.</TD></TR></TABLE></CENTER><P><P>Normally, if the match list is longer than the replacement list,the last character in the replacement list is used as the replacementfor the extra characters. However, when the d option is used,the matched characters simply are deleted.<P>If the replacement list is empty, then no translation is done.The operator still will return the number of characters that matched,though. This is useful when you need to know how often a givenletter appears in a string. This feature also can compress repeatedcharacters using the <TT>s</TT> option.<BR><p><CENTER><TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%><TR><TD><B>Tip</B></TD></TR><TR><TD><BLOCKQUOTE>UNIX programmers may be familiar with using the <TT>tr</TT> utility to convert lowercase characters to uppercase characters, or vice versa. Perl now has the <TT>lc()</TT> and <TT>uc()</TT> fuNCtions that can do this much quicker.</BLOCKQUOTE></TD></TR></TABLE></CENTER><P><H2><A NAME="TheBindingOperatorsand"><FONT SIZE=5 COLOR=#FF0000>The Binding Operators (=~ and !~)</FONT></A></H2><P>The search, modify, and translation operations work on the <TT>$_</TT>variable by default. What if the string to be searched is in someother variable? That's where the binding operators come into play.They let you bind the regular expression operators to a variableother than <TT>$_</TT>. There aretwo forms of the binding operator: the regular <TT>=~</TT>and its complement <TT>!~</TT>. Thefollowing small program shows the syntax of the <TT>=~</TT>operator:<BLOCKQUOTE><PRE>$scalar = "The root has many leaves";$match = $scalar =~ m/root/;$substitution = $scalar =~ s/root/tree/;$translate = $scalar =~ tr/h/H/;print("\$match = $match\n");print("\$substitution = $substitution\n");print("\$translate = $translate\n");print("\$scalar = $scalar\n");</PRE></BLOCKQUOTE><P>This program displays the following:<BLOCKQUOTE><PRE>$match = 1$substitution = 1$translate = 2$scalar = The tree has many leaves</PRE></BLOCKQUOTE><P>This example uses all three of the regular expression operatorswith the regular binding operator. Each of the regular expressionoperators was bound to the <TT>$scalar</TT>variable instead of <TT>$_</TT>. Thisexample also shows the return values of the regular expressionoperators. If you don't need the return values, you could do this:<BLOCKQUOTE><PRE>$scalar = "The root has many leaves";print("String has root.\n") if $scalar =~ m/root/;$scalar =~ s/root/tree/;$scalar =~ tr/h/H/;print("\$scalar = $scalar\n");</PRE></BLOCKQUOTE><P>This program displays the following:<BLOCKQUOTE><PRE>String has root.$scalar = The tree has many leaves</PRE></BLOCKQUOTE><P>The left operand of the binding operator is the string to be searched,modified, or transformed; the right operand is the regular expressionoperator to be evaluated. The complementary binding operator isvalid only when used with the matching regular expression operator.If you use it with the substitution or translation operator, youget the following message if you're using the <TT>-w</TT>command-line option to run Perl:<BLOCKQUOTE><PRE>Useless use of not in void context at test.pl line 4.</PRE></BLOCKQUOTE><P>You can see that the <TT>!~</TT> isthe opposite of <TT>=~</TT> by replacingthe <TT>=~</TT> in the previous example:<BLOCKQUOTE><PRE>$scalar = "The root has many leaves";print("String has root.\n") if $scalar !~ m/root/;$scalar =~ s/root/tree/;$scalar =~ tr/h/H/;print("\$scalar = $scalar\n");</PRE></BLOCKQUOTE><P>This program displays the following:<BLOCKQUOTE><PRE>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -