📄 ch7.htm
字号:
<HTML>
<HEAD>
<TITLE>Chapter 7 -- String and Patterns</TITLE>
<META NAME="GENERATOR" CONTENT="Mozilla/3.0b5aGold (WinNT; I) [Netscape]">
</HEAD>
<BODY TEXT="#000000" BGCOLOR="#FFFFFF" LINK="#0000EE" VLINK="#551A8B" ALINK="#CE2910">
<H1><FONT COLOR=#FF0000>Chapter 7</FONT></H1>
<H1><B><FONT SIZE=5 COLOR=#FF0000>String and Patterns</FONT></B>
</H1>
<P>
<HR WIDTH="100%"></P>
<P>
<H3 ALIGN=CENTER><FONT COLOR="#000000"><FONT SIZE=+2>CONTENTS<A NAME="CONTENTS"></A>
</FONT></FONT></H3>
<UL>
<LI><A HREF="#BasicStringOperations" >Basic String Operations</A>
<UL>
<LI><A HREF="#ThechopandlengthFunctions" >The chop() and length() Functions</A>
<LI><A HREF="#HandlingtheCaseinStrings" >Handling the Case in Strings</A>
<LI><A HREF="#JoiningStringsTogether" >Joining Strings Together</A>
<LI><A HREF="#PrintingFormattedNumbers" >Printing Formatted Numbers</A>
</UL>
<LI><A HREF="#FindingSubstrings" >Finding Substrings</A>
<UL>
<LI><A HREF="#ThesubstrFunction" >The substr Function</A>
<LI><A HREF="#StringSearchingwithPatterns" >String Searching with Patterns</A>
<LI><A HREF="#SpecialCharactersinPerlPatternSearc" >Special Characters in Perl Pattern Searches</A>
<LI><A HREF="#ShortcutsforWordsinPerl" >Shortcuts for Words in Perl</A>
<LI><A HREF="#ThequotemetaFunction" >The quotemeta Function</A>
<LI><A HREF="#SpecifyingtheNumberofMatches" >Specifying the Number of Matches</A>
<LI><A HREF="#SpecifyingMoreThanOneChoice" >Specifying More Than One Choice</A>
<LI><A HREF="#SearchingaStringforMoreThanOnePat" >Searching a String for More Than One Pattern to Match</A>
</UL>
<LI><A HREF="#ReusingPortionsofPatterns" >Reusing Portions of Patterns</A>
<LI><A HREF="#PatternMatchingOptions" >Pattern-Matching Options</A>
<LI><A HREF="#SubstitutingTextThroughPatternMatchi" >Substituting Text Through Pattern Matching </A>
<LI><A HREF="#TheTranslationOperator" >The Translation Operator </A>
<LI><A HREF="#ExtendedPatternMatching" >Extended Pattern Matching </A>
<LI><A HREF="#Summary" >Summary </A>
</UL>
<HR>
<P>
This chapter covers some of the most important features of Perl:
its string- and pattern-manipulation routines. Most of the Perl
programming you do will involve strings in one form or another.
It's very important to learn how to use the string search and
replace operations efficiently in Perl. An inefficient search
pattern can slow a script down to a crawl.
<H2><A NAME="BasicStringOperations"><FONT SIZE=5 COLOR=#FF0000>Basic
String Operations</FONT></A></H2>
<P>
Let's first start with the basic operations in Perl for working
with strings. Some of this chapter will be a rehash of what was
covered in <A HREF="ch2.htm" tppabs="http://www.mcp.com/815097600/0-672/0-672-30891-6/ch2.htm" >Chapters 2</A> through <A HREF="ch5.htm" tppabs="http://www.mcp.com/815097600/0-672/0-672-30891-6/ch5.htm" >5</A>.
Now it's time to cover the topic in detail given the background
information in these chapters. I cover the following string utility
functions in this chapter:
<UL>
<LI><FONT COLOR=#000000>The </FONT><TT><FONT FACE="Courier">chop()</FONT></TT>
and <TT><FONT FACE="Courier">length()</FONT></TT> functions
<LI><FONT COLOR=#000000>Handling the case in strings</FONT>
<LI><FONT COLOR=#000000>Joining strings together</FONT>
<LI><FONT COLOR=#000000>Printing formatted numbers</FONT>
<LI><FONT COLOR=#000000>The </FONT><TT><FONT FACE="Courier">substr()</FONT></TT>
function
<LI><FONT COLOR=#000000>Special characters in Perl pattern searches</FONT>
<LI><FONT COLOR=#000000>Shortcuts for special words in Perl</FONT>
<LI><FONT COLOR=#000000>The </FONT><TT><FONT FACE="Courier">quotemeta()</FONT></TT>
function
<LI><FONT COLOR=#000000>Specifying the number of matches</FONT>
<LI><FONT COLOR=#000000>Specifying more than one choice in a pattern</FONT>
<LI><FONT COLOR=#000000>Searching a string with more than one
pattern</FONT>
</UL>
<H3><A NAME="ThechopandlengthFunctions">The <TT><FONT SIZE=4 FACE="Courier">chop()</FONT></TT><FONT SIZE=4>
and </FONT><TT><FONT SIZE=4 FACE="Courier">length()</FONT></TT><FONT SIZE=4>
Functions</FONT></A></H3>
<P>
To find the length of a string, you can call the <TT><FONT FACE="Courier">length($str)</FONT></TT>
function, which returns the number of characters in a string.
The <TT><FONT FACE="Courier">chop</FONT></TT> function removes
the last character in a string. This is useful in removing the
carriage return from a user-entered string. For an example, see
Listing 7.1.
<HR>
<BLOCKQUOTE>
<B>Listing 7.1. Using </B><TT><B><FONT FACE="Courier">length()</FONT></B></TT><B>
and </B><TT><B><FONT FACE="Courier">chop()</FONT></B></TT><B>.
<BR>
</B>
</BLOCKQUOTE>
<BLOCKQUOTE>
<TT><FONT FACE="Courier"> 1 #!/usr/bin/perl<BR>
2 <BR>
3 $input = <STDIN>
;<BR>
4 <BR>
5 $len = length($input);
<BR>
6 print "\nLength = $len of $input before the chopping
\n";<BR>
7 chop($input);<BR>
8 $len = length($input);<BR>
9 print "\nLength
= $len of $input after the chopping \n";<BR>
10 <BR>
11 $ 7_1.pl<BR>
12 Hello! I am a Test!<BR>
13 <BR>
14 Length = 20 of Hello! I am a Test! before the chopping<BR>
15 <BR>
16 Length = 19 of Hello! I am a Test! after the chopping</FONT></TT>
</BLOCKQUOTE>
<HR>
<H3><A NAME="HandlingtheCaseinStrings">Handling the Case in Strings</A>
</H3>
<P>
Perl provides four functions to make your life easier when handling
the case of characters in a string:<P>
<CENTER>
<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>
<TR VALIGN=TOP><TD WIDTH=177><TT><FONT FACE="Courier">lc($string)</FONT></TT>
</TD><TD WIDTH=347>Converts a string to lowercase</TD></TR>
<TR VALIGN=TOP><TD WIDTH=177><TT><FONT FACE="Courier">uc($string)</FONT></TT>
</TD><TD WIDTH=347>Converts a string to uppercase</TD></TR>
<TR VALIGN=TOP><TD WIDTH=177><TT><FONT FACE="Courier">lcfirst($string)</FONT></TT>
</TD><TD WIDTH=347>Converts the first character of a string to uppercase
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=177><TT><FONT FACE="Courier">ucfirst($string)</FONT></TT>
</TD><TD WIDTH=347>Converts the first character of a string to lowercase
</TD></TR>
</TABLE></CENTER>
<P>
<P>
Listing 7.2 presents sample code that illustrates how these functions
work.
<HR>
<BLOCKQUOTE>
<B>Listing 7.2. Using the case functions.<BR>
</B>
</BLOCKQUOTE>
<BLOCKQUOTE>
<TT><FONT FACE="Courier"> 1 #!/usr/bin/perl<BR>
2 $name = "tis a test OF THE sYSTEm" ;<BR>
3 $ucase = uc($name);
<BR>
4 $lcase = lc($name);<BR>
5 <BR>
6 print "$name \n";<BR>
7 print "$ucase
\n";<BR>
8 print "$lcase \n";<BR>
9 <BR>
10 $nice = lcfirst($ucase);<BR>
11 print "lcfirst on $ucase = \n\t $nice \n";<BR>
12 <BR>
13 $crooked = ucfirst($lcase);<BR>
14 print "ucfirst on $lcase = \n\t$crooked \n";</FONT></TT>
</BLOCKQUOTE>
<HR>
<P>
Here is the output from Listing 7.2.
<BLOCKQUOTE>
<TT><FONT FACE="Courier">tis a test OF THE sYSTEm<BR>
TIS A TEST OF THE SYSTEM<BR>
tis a test of the system<BR>
lcfirst on TIS A TEST OF THE SYSTEM =<BR>
</FONT></TT> <TT><FONT FACE="Courier">
tIS A TEST OF THE SYSTEM<BR>
ucfirst on tis a test of the system =<BR>
Tis a test of the system</FONT></TT>
</BLOCKQUOTE>
<H3><A NAME="JoiningStringsTogether">Joining Strings Together</A>
</H3>
<P>
The dot operator is great for connecting strings. For example,
the following statements will print <TT><FONT FACE="Courier">John
Hancock</FONT></TT> plus a new line:
<BLOCKQUOTE>
<TT><FONT FACE="Courier">$first="John";<BR>
$last ="Hancock";<BR>
print $first . " " . $last . "\n" ;</FONT></TT>
</BLOCKQUOTE>
<P>
To print the elements of an array in a string, you can use the
<TT><FONT FACE="Courier">join</FONT></TT> function to create one
long string. Here's the syntax for the <TT><FONT FACE="Courier">join</FONT></TT>
function:
<BLOCKQUOTE>
<TT><FONT FACE="Courier">join (<I>$joinstr, @list</I>);</FONT></TT>
</BLOCKQUOTE>
<P>
The <TT><FONT FACE="Courier">$joinstr</FONT></TT> variable is
the string to use when connecting the elements of <TT><FONT FACE="Courier">@list</FONT></TT>
together. Refer to the following statements to see how to create
a line for the <TT><FONT FACE="Courier">/etc/passwd</FONT></TT>
file:
<BLOCKQUOTE>
<TT><FONT FACE="Courier">@list = ("khusain","sdfsdew422dxd","501","100",
<BR>
"Kamran Husain","/home/khusain","/bin/bash");
<BR>
$passwdEntry = join (":", @list);</FONT></TT>
</BLOCKQUOTE>
<H3><A NAME="PrintingFormattedNumbers">Printing Formatted Numbers</A>
</H3>
<P>
Perl provides two functions, <TT><FONT FACE="Courier">printf</FONT></TT>
and <TT><FONT FACE="Courier">sprintf</FONT></TT>, that behave
like the <TT><FONT FACE="Courier">printf</FONT></TT> family of
functions in the C programming language. The <TT><FONT FACE="Courier">printf</FONT></TT>
function sends its output to the current file. The <TT><FONT FACE="Courier">sprintf</FONT></TT>
function takes at least two arguments, a string and a format string,
followed by any other arguments. The <TT><FONT FACE="Courier">sprintf</FONT></TT>
function sends the formatted output to the string in the first
argument. For example, the string <TT><FONT FACE="Courier">$answer</FONT></TT>
contains the result of the <TT><FONT FACE="Courier">sprintf</FONT></TT>
statement:
<BLOCKQUOTE>
<TT><FONT FACE="Courier">$a= 10;<BR>
$b= 10;<BR>
sprintf $answer, "%d + %d is %d and in %x", $a,$b,$a+$b,$a+$b;</FONT></TT>
</BLOCKQUOTE>
<H2><A NAME="FindingSubstrings"><FONT SIZE=5 COLOR=#FF0000>Finding
Substrings</FONT></A></H2>
<P>
A quick way to find the location of a substring in a string is
to use the <TT><FONT FACE="Courier">index</FONT></TT> function,
which searches from left to right. To search from right to left,
use the <TT><FONT FACE="Courier">rindex</FONT></TT> function.
Here's the syntax for these functions:
<BLOCKQUOTE>
<TT><FONT FACE="Courier">position = index ($string, $substring,
[$offset]);<BR>
position = rindex ($string, $substring, [$offset]);</FONT></TT>
</BLOCKQUOTE>
<P>
<TT><FONT FACE="Courier">$string</FONT></TT> is the character
string to search the <TT><FONT FACE="Courier">$substring</FONT></TT>
in. The <TT><FONT FACE="Courier">$offset</FONT></TT> parameter
is optional and defaults to the start of the string when not provided
to the function. Listing 7.3 is a function that looks for the
position of the word <I>two</I> in each line of an input file
(just like <TT><FONT FACE="Courier">grep</FONT></TT> would except
that we print out the position of the character, too).
<HR>
<BLOCKQUOTE>
<B>Listing 7.3. Using the </B><TT><B><FONT FACE="Courier">index</FONT></B></TT><B>
and </B><TT><B><FONT FACE="Courier">rindex</FONT></B></TT><B>
functions.<BR>
</B>
</BLOCKQUOTE>
<BLOCKQUOTE>
<TT><FONT FACE="Courier"> 1 #!/usr/bin/perl<BR>
2 <BR>
3 %finds = ();<BR>
4 $line = 0;<BR>
5 <BR>
6 print "\n Enter word to search for:";<BR>
7 $word = <STDIN>;
<BR>
8 chop ($word);<BR>
9 <BR>
10 print "\n Enter file to search in:";<BR>
11 $fname = <STDIN>;<BR>
12 chop($fname);<BR>
13 open (IFILE, $fname) || die "Cannot open $fname $!\n";
<BR>
14 <BR>
15 while (<IFILE>) {<BR>
16 $position = index($_,$word);<BR>
17 if ($position >= 0) {<BR>
18 $finds{"$line"}
= $position;<BR>
19 }<BR>
20 $line++;<BR>
21 }<BR>
22 close IFILE;<BR>
23 while(($key,$value) = each(%finds)) {<BR>
24 print " Line $key : $value \n";
<BR>
25 }</FONT></TT>
</BLOCKQUOTE>
<HR>
<P>
This program searches for the first occurrence of the word in
the file specified by the user. Each line in the file is searched
for the pattern. If the pattern is found, the program prints the
location of the pattern at each line and column number. The first
<TT><FONT FACE="Courier">while</FONT></TT> loop searches in a
given file, and the second <TT><FONT FACE="Courier">while</FONT></TT>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -