這一章是很重要的苗踪,幾乎整章都是要點母蛛,而且知識點比較零碎萧锉,但是很好理解。
課后練習(xí)
第一題
Make a program that prints each line of its input that mentions fred. (It
shouldn’t do anything for other lines of input.) Does it match if your input string
is Fred, frederick, or Alfred? Make a small text file with a few lines mentioning
“fred flintstone” and his friends, then use that file as input to this program and
the ones later in this section.
這道題是說首先創(chuàng)建一個小文本,里面要包含fred flintstone這個字符串。并且還要包括其他的名字字符串息裸。然后檢查每一行里是否有fred這個字符串可以匹配蝇更。
腳本:
#!/usr/bin/perl
use warnings;
use strict;
use v5.24;
while($_= <>){
chomp;
if (m/fred/){
print "This line contains the word \"fred\":\n$_\n";
}
}
小文本text1.txt:
$ cat text1.txt
His first name is Fred.
His full name if Fred Flintsone.
He is a student whoes major in computer science.
He has a lot of friends in school.One of his friends named Frederick.
The other friend of him is Alfred.
運行腳本:
$ ./practice1.pl text1.txt #運行腳本,查找text1.txt文件里是否有fred字符串呼盆,發(fā)現(xiàn)只有最后一行才有年扩。其他行都是Fred,說明了匹配操作是區(qū)分大小寫的访圃。
This line contains the word "fred":
The other friend of him is Alfred.
第二題
Modify the previous program to allow Fred to match as well. Does it match
now if your input string is Fred, frederick, or Alfred? (Add lines with these
names to the text file.) 本題要求讓fred和Fred都可以和文本匹配上厨幻。
腳本:
#!/usr/bin/perl
use warnings;
use strict;
use v5.24;
while($_= <>){
chomp;
if (m/[Ff]red/){
print "$_\n";
}
}
運行:
$ ./practice1.pl text1.txt
His first name is Fred.
His full name if Fred Flintsone.
He has a lot of friends in school.One of his friends named Frederick.
The other friend of him is Alfred.
第三題
Make a program that prints each line of its input that contains a period (.),
ignoring other lines of input. Try it on the small text file from the previous exercise:
does it notice Mr. Slate?
根據(jù)題目要求,先把小文本的內(nèi)容進行一下修改腿时,隨便改了一些况脆,使文本里包含Mr.Slate這個字符串:
$ cat text1.txt
His first name is Fred.
Mr.Slate is his father.
His full name if Fred Flintsone.
He is a student whoes major in computer science.
He has a lot of friends in school.
One of his friends named Frederick.
The other friend of him is Alfred.
腳本:
#!/usr/bin/perl
use warnings;
use strict;
use v5.24;
while($_= <>){
chomp;
if (m/[.]./){ #方括號里的點代表字符點,后面的那個點代表在字符點后面還要匹配一個任意字符(除了新行)批糟,這就使得小文本里的其他行最后的句號不符合這個要求格了,所以小文本里能匹配的只有一句。
print "$_\n"; #打印符合要求的那一行
}
}
運行:
$ ./practice1.pl text1.txt
Mr.Slate is his father.
第四題
Make a program that prints each line that has a word that is capitalized but
not ALL capitalized. Does it match Fred but neither fred nor FRED?本題要求只匹配第一個字母大寫的fred徽鼎,而不能匹配全是小寫或者全是大寫的字符盛末。
修改小文本:
$ cat text1.txt
fred
Fred
FRED
fred flintstone
Fred Flintstone
frederick
腳本:
#!/usr/bin/perl
use warnings;
use strict;
use v5.24;
while($_= <>){
chomp;
if (m/[A-Z][a-z]+/){
print "$_\n";
}
}
運行:
$ ./practice1.pl text1.txt
Fred
Fred Flintstone
第五題
Make a program that prints each line that has two of the same nonwhitespace
characters next to each other. It should match lines that contain words such as
Mississippi, Bamm-Bamm, or llama.本題要求匹配打印出有連續(xù)2個相同字符(非空白字符)的行
修改小文本:
$ cat text1.txt
fred
Mississippi
Bamm-Bamm
Alfred
frederick
llama
腳本:
#!/usr/bin/perl
use warnings;
use strict;
use v5.24;
while($_= <>){
chomp;
if (m/(.)\1/){ #括號里加點,后面再加一個\1代表再次匹配前面的點匹配的字符否淤,換句話說悄但,就是要匹配兩個連續(xù)的字符
print "$_\n";
}
}
運行:
$ ./practice1.pl text1.txt
Mississippi
Bamm-Bamm
llama
第六題
Extra credit exercise: write a program that prints out any input line that mentions both wilma and fred.
修改小文本:
$ cat text1.txt
fred
wilma
Alfred
fred and wilma
腳本:
#!/usr/bin/perl
use warnings;
use strict;
use v5.24;
while($_= <>){
chomp;
if (m/fred/ && /wilma/){ #注意這里兩個//和&之間是有空格的,兩個&&代表“并且”的意思
print "$_\n";
}
}
運行:
$ ./practice1.pl text1.txt
fred and wilma