unx16.htm
来自「Unix Unleashed, Third Edition is written」· HTM 代码 · 共 2,814 行 · 第 1/5 页
HTM
2,814 行
is assigned 15; if $x is not zero, $y is assigned 8.
<BR></P>
<H5 ALIGN="CENTER">
<CENTER><A ID="I29" NAME="I29">
<FONT SIZE=3><B>Matching Patterns</B>
<BR></FONT></A></CENTER></H5>
<P>Perl allows you to examine scalar variables and test for the existence of a particular pattern in a string. To do this, use the =~ (pattern matching) operator:
<BR></P>
<PRE>$x =~ /jkl/</PRE>
<P>The character string enclosed by the / characters is the pattern to be matched, and the scalar variable on the left of the =~ operator is the variable to be examined. This example searches for the pattern jkl in the scalar variable $x. If $x contains
jkl, the expression is true; if not, the expression is false. In the statement $y = $x =~ /jkl/;, $y is assigned a non-zero value if $x contains jkl, and is assigned zero if $x does not contain jkl.
<BR></P>
<P>The !~ operator is the negation of =~:
<BR></P>
<PRE>$y = $x !~ /jkl/;</PRE>
<P>Here, $y is assigned zero if $x contains jkl, and a non-zero value otherwise.
<BR></P>
<H6 ALIGN="CENTER">
<CENTER>
<FONT SIZE=3><B>Using Special Characters in Patterns</B>
<BR></FONT></CENTER></H6>
<P>You can use several special characters in your patterns. The * character matches zero or more of the character it follows:
<BR></P>
<PRE>/jk*l/</PRE>
<P>This matches jl, jkl, jkkl, jkkkl, and so on.
<BR></P>
<P>The + character matches one or more of the preceding character:
<BR></P>
<PRE>/jk+l/</PRE>
<P>This matches jkl, jkkl, jkkkl, and so on.
<BR></P>
<P>The ? character matches zero or one copies of the preceding character:
<BR></P>
<PRE>/jk?l/</PRE>
<P>This matches jl or jkl.
<BR></P>
<P>The character . matches any character except the newline character:
<BR></P>
<PRE>/j.l/</PRE>
<P>This matches any pattern consisting of a j, any character, and an l.
<BR></P>
<P>If a set of characters is enclosed in square brackets, any character in the set is an acceptable match:
<BR></P>
<PRE>/j[kK]l/ # matches jkl or jKl</PRE>
<P>Consecutive alphanumeric characters in the set can be represented by a dash (-):
<BR></P>
<PRE>/j[k1-3K]l/ # matches jkl, j1l, j2l, j3l or jKl</PRE>
<P>You can specify that a match must be at the start or end of a line by using ^ or $:
<BR></P>
<PRE>/^jkl/ # matches jkl at start of line
/jkl$/ # matches jkl at end of line
/^jkl$/ # matches line consisting of exactly jkl</PRE>
<P>You can specify that a match must be either on a word boundary or inside a word by including \b or \B in the pattern:
<BR></P>
<PRE>/\bjkl/ # matches jkl, but not ijkl
/\Bjkl/ # matches ijkl, but not jkl</PRE>
<P>Some sets are so common that special characters exist to represent them:
<BR></P>
<UL>
<LI>\d matches any digit, and is equivalent to [0-9].
<BR>
<BR></LI>
<LI>\w matches any character that can appear in a variable name; it is equivalent to [A-Za-z_0-9].
<BR>
<BR></LI>
<LI>\s matches any whitespace (any character not visible on the screen); it is equivalent to [ \r\t\n\f]. (These backslash characters were explained in "Using Double- and Single-Quoted Strings" earlier in this chapter.)
<BR>
<BR></LI></UL>
<P>To match all but a specified set of characters, specify ^ at the start of your set:
<BR></P>
<PRE>/j[^kK]l/</PRE>
<P>This matches any string containing j, any character but k or K, and l.
<BR></P>
<P>To use a special character as an ordinary character, precede it with a backslash (\):
<BR></P>
<PRE>/j\*l/ # this matches j*l</PRE>
<P>This matches j*l.
<BR></P>
<P>In patterns, the * and + special characters match as many characters in a string as possible. For example, consider the following:
<BR></P>
<PRE>$x = "abcde";
$y = $x =~ /a.*/;</PRE>
<P>The pattern /a.*/ can match a, ab, abc, abcd, or abcde. abcde is matched, since it is the longest. This becomes meaningful when patterns are used in substitution.
<BR></P>
<H6 ALIGN="CENTER">
<CENTER>
<FONT SIZE=3><B>Substituting and Translating Using Patterns</B>
<BR></FONT></CENTER></H6>
<P>You can use the =~ operator to substitute one string for another:
<BR></P>
<PRE>$val =~ s/abc/def/; # replace abc with def
$val =~ s/a+/xyz/; # replace a, aa, aaa, etc., with xyz
$val =~ s/a/b/g; # replace all a's with b's</PRE>
<P>Here, the s prefix indicates that the pattern between the first / and the second is to be replaced by the string between the second / and the third.
<BR></P>
<P>You can also translate characters using the tr prefix:
<BR></P>
<PRE>$val =~ tr/a-z/A-Z/; # translate lower case to upper</PRE>
<P>Here, any character matched by the first pattern is replaced by the corresponding character in the second pattern.
<BR></P>
<H5 ALIGN="CENTER">
<CENTER><A ID="I30" NAME="I30">
<FONT SIZE=3><B>The Order of Operations</B>
<BR></FONT></A></CENTER></H5>
<P>Consider the following statement:
<BR></P>
<PRE>$a = 21 * 2 + 3 << 1 << 2 ** 2;</PRE>
<P>The problem: Which operation should be performed first?
<BR></P>
<P>The following sections answer questions of this type.
<BR></P>
<H6 ALIGN="CENTER">
<CENTER>
<FONT SIZE=3><B>Precedence</B>
<BR></FONT></CENTER></H6>
<P>In standard grade-school arithmetic, certain operations are always performed before others. For example, multiplication is always performed before addition:
<BR></P>
<PRE>4 + 5 * 3</PRE>
<P>Because multiplication is performed before addition, it has higher precedence than addition.
<BR></P>
<P>Table 16.2 defines the precedence of the Perl operators described in these sections. The items at the top of the table have the highest precedence, and the items at the bottom have the lowest.
<BR></P>
<UL>
<LH><B>Table 16.2. Operator Precedence in Perl.</B>
<BR></LH></UL>
<TABLE BORDER>
<TR>
<TD>
<P>++, —</P>
<TD>
<P>Autoincrement and autodecrement</P>
<TR>
<TD>
<P>-, ~, !</P>
<TD>
<P>Operators with one operand</P>
<TR>
<TD>
<P>**</P>
<TD>
<P>Exponentiation</P>
<TR>
<TD>
<P>=~, !~</P>
<TD>
<P>Matching operators</P>
<TR>
<TD>
<P>*, /, %, x</P>
<TD>
<P>Multiplication, division, remainder, repetition</P>
<TR>
<TD>
<P>+, -, .</P>
<TD>
<P>Addition, subtraction, concatenation</P>
<TR>
<TD>
<P><<, >></P>
<TD>
<P>Shifting operators</P>
<TR>
<TD>
<P>-e, -r, etc.</P>
<TD>
<P>File status operators</P>
<TR>
<TD>
<P><, <=, >, >=, lt, le, gt, ge</P>
<TD>
<P>Inequality comparison operators</P>
<TR>
<TD>
<P>==, !=, <=>, eq, ne, cmp</P>
<TD>
<P>Equality comparison operators</P>
<TR>
<TD>
<P>&</P>
<TD>
<P>Bitwise AND</P>
<TR>
<TD>
<P>|, ^</P>
<TD>
<P>Bitwise OR and exclusive OR</P>
<TR>
<TD>
<P>&&</P>
<TD>
<P>Logical AND</P>
<TR>
<TD>
<P>||</P>
<TD>
<P>Logical OR</P>
<TR>
<TD>
<P>..</P>
<TD>
<P>List range operator</P>
<TR>
<TD>
<P>? and :</P>
<TD>
<P>Conditional operator</P>
<TR>
<TD>
<P>=, +=, -=, *=, etc.</P>
<TD>
<P>Assignment operators</P>
<TR>
<TD>
<P>,</P>
<TD>
<P>Comma operator</P></TABLE>
<P>For example, consider the following statement:
<BR></P>
<PRE>$x = 11 * 2 + 6 ** 2 << 2;</PRE>
<P>The operations in this statement are performed in the following order:
<BR></P>
<OL>
<LI>6 ** 2, yielding 36.
<BR>
<BR></LI>
<LI>11 * 2, yielding 22.
<BR>
<BR></LI>
<LI>36 + 22, yielding 58.
<BR>
<BR></LI>
<LI>58 << 2, yielding 116.
<BR>
<BR></LI></OL>
<P>Therefore, 116 is assigned to $x.
<BR></P>
<P>This operator precedence table contains some operators that are defined in later sections. The .. (list range) operator is defined in "Using Lists and Array Variables." The file status operators are described in "Reading from and Writing
to Files."
<BR></P>
<H6 ALIGN="CENTER">
<CENTER>
<FONT SIZE=3><B>Associativity</B>
<BR></FONT></CENTER></H6>
<P>Consider the following statement:
<BR></P>
<PRE>$x = 2 + 3 - 4;</PRE>
<P>In this case, it doesn't matter whether the addition (2 + 3) or the subtraction (3 - 4) is performed first, because the result is the same either way. However, for some operations, the order of evaluation makes a difference:
<BR></P>
<PRE>$x = 2 ** 3 ** 2;</PRE>
<P>Is $x assigned 64 (8 ** 2) or 512 (2 ** 9)?
<BR></P>
<P>To resolve these problems, Perl associates a specified associativity with each operator. If an operator is right-associative, the rightmost operator is performed first when two operators have the same precedence:
<BR></P>
<PRE>$x = 2 ** 3 ** 2; # the same as $x = 2 ** 9, or $x = 512</PRE>
<P>If an operator is left-associative, the leftmost operator is performed first when two operators have the same precedence:
<BR></P>
<PRE>$x = 29 % 6 * 2; # the same as $x = 5 * 2, or $x = 10</PRE>
<P>The following operators in Perl are right-associative:
<BR></P>
<UL>
<LI>The assignment operators (=, +=, and so on)
<BR>
<BR></LI>
<LI>The ? and : operator combination
<BR>
<BR></LI>
<LI>The ** operator (exponentiation)
<BR>
<BR></LI>
<LI>The operators that have only one operand (!, ~ and -)
<BR>
<BR></LI></UL>
<P>All other operators are left-associative.
<BR></P>
<H6 ALIGN="CENTER">
<CENTER>
<FONT SIZE=3><B>Forcing Precedence Using Parentheses</B>
<BR></FONT></CENTER></H6>
<P>Perl allows you to force the order of evaluation of operations in expressions. To do this, use parentheses:
<BR></P>
<PRE>$x = 4 * (5 + 3);</PRE>
<P>In this statement, 5 is added to 3 and then multiplied by 4, yielding 32.
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?