📄 ch10.htm
字号:
<CENTER><B>Table 10.2 Options for the Matching Operator</B></CENTER>
<p>
<CENTER>
<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>
<TR><TD WIDTH=73><CENTER><I>Option</I></CENTER></TD><TD WIDTH=517><I>Description</I>
</TD></TR>
<TR><TD WIDTH=73><CENTER>g</CENTER></TD><TD WIDTH=517>This option finds all occurreNCes of the pattern in the string. A list of matches is returned or you can iterate over the matches using a loop statement.
</TD></TR>
<TR><TD WIDTH=73><CENTER>i</CENTER></TD><TD WIDTH=517>This option ignores the case of characters in the string.
</TD></TR>
<TR><TD WIDTH=73><CENTER>m</CENTER></TD><TD WIDTH=517>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=73><CENTER>o</CENTER></TD><TD WIDTH=517>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=73><CENTER>s</CENTER></TD><TD WIDTH=517>This option treats the string as a single line.
</TD></TR>
<TR><TD WIDTH=73><CENTER>x</CENTER></TD><TD WIDTH=517>This option lets you use extended regular expressions. Basically, this means that Perl will ignore white space that's 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>
All options are specified after the last pattern delimiter. For
instaNCe, if you want the match to ignore the case of the characters
in 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 lowercase
and 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 array
variable or used inside a loop. This feature comes in handy after
you learn about meta-characters in the section called "How
to 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 we
start 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 operator
just 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 1234567890
AAA"</TT> instead of its original value, and the <TT>$result</TT>
variable will be equal to <TT>1-</TT>the
number 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 effectively
deleted it.
<P>
If brackets of any type are used as delimiters for the search
pattern, you need to use a second set of brackets to eNClose the
replacement 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 several
options. One interesting option is the capability to evaluate
the replacement pattern as an expression instead of a string.
You could use this capability to find all numbers in a file and
multiply them by a given percentage, for instaNCe. Or, you could
repeat matched strings by using the string repetition operator.
Table 10.3 shows all of the options you can use with the substitution
operator.<BR>
<P>
<CENTER><B>Table 10.3 Options for the Substitution
Operator</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 the
interpretation of the pattern delimiters. If used, variable interpolation
is active even if single quotes are used. In addition, if back
quotes are used as delimiters, the replacement pattern is executed
as a DOS or UNIX command. The output of the command then is used
as 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 more
than one character in the match character list, you can translate
multiple 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 than
the target list of characters, the last character in the replacement
list is repeated as often as needed. However, if more than one
replacement character is given for a matched character, only the
first is used. For instaNCe:
<BLOCKQUOTE>
<PRE>
tr/WWW/ABC/;
</PRE>
</BLOCKQUOTE>
<P>
results in all <TT>W</TT> characters
being converted to an <TT>A</TT> character.
The rest of the replacement list is ignored.
<P>
Unlike the matching and substitution operators, the translation
operator 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 matching
and substitution operators. You can delete matched characters,
replace repeated characters with a single character, and translate
only characters that don't match the character list. Table 10.4
shows 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 replacement
for 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 given
letter appears in a string. This feature also can compress repeated
characters 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 some
other variable? That's where the binding operators come into play.
They let you bind the regular expression operators to a variable
other than <TT>$_</TT>. There are
two forms of the binding operator: the regular <TT>=~</TT>
and its complement <TT>!~</TT>. The
following 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 operators
with the regular binding operator. Each of the regular expression
operators was bound to the <TT>$scalar</TT>
variable instead of <TT>$_</TT>. This
example also shows the return values of the regular expression
operators. 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");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -