Learning Perl学习笔记(6)第七章 正则表达式

这一章是很重要的,几乎整章都是要点,而且知识点比较零碎,但是很好理解。

课后练习

第一题

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.
这道题是说首先创建一个小文本,里面要包含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字符串,发现只有最后一行才有。其他行都是Fred,说明了匹配操作是区分大小写的。
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?

根据题目要求,先把小文本的内容进行一下修改,随便改了一些,使文本里包含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.本题要求匹配打印出有连续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代表再次匹配前面的点匹配的字符,换句话说,就是要匹配两个连续的字符
                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

你可能感兴趣的:(Learning Perl学习笔记(6)第七章 正则表达式)