学习Perl (5)
今天下午干别的事情去了
以正则表达式进行匹配
1)以m//进行匹配
以/i来进行不区分大小写的模式匹配:
#!/usr/bin/perl -w
print "Would you like to play a game?";
chomp($_ =
if (/yes/i) {
print "In that case,I recommend that you go bowling.n";
}
以/s来匹配任意字符:
#!/usr/bin/perl -w
$_ = "I saw Barneyndown at the bowling alleynwith frednlast night.n";
if (/Barney.*Fred/s) {
print "That string mentions Fred after Barney!n";
}
用/x来加上空白:
/ -? d+ .? d* /x
组合选项修饰符:
#!/usr/bin/perl -w
while (<>) {
if (/barney.*fred/si) {
print "That string mentions Fred after Barney!n";
}
}
2)锚点
插入标记(^)是一个锚点,用来标示字符串的开头,而美元符号($)则用来标示字符串的结尾,如:/^fred/ 和 /rock$/。/^s*$/,这代表一个空白行。
单词锚点:b是单词边界锚点,它所匹配的是一个单词头尾两端,如:/bfredb/。此处所谓的“单词“是指一连串的字母、数字和下划线的组合,也就是匹配/w+/模式的字符串。
B是非单词边界符,它会匹配任何不匹配b的位置,如:/bserchB/。
绑定操作符=~
用来告诉Perl拿右边的模式来匹配左边的字符串:
#!/usr/bin/perl -w
print "Do you like Perl?(yes/no)";
my $likes_perl = (
if ($likes_perl) {
print "You said earlier that you like Perl,so ...n";
}
模式内的内插:正则表达式里可以进行双引号形式的内插,类似grep的程序:
#!/usr/bin/perl -w
my $what = "larry";
while (<>) {
if (/^($what)/) {
print "We saw $what in beginning of:$_";
}
}
3)匹配变量
匹配变量所存储的都是字符串,所以它们都是标量变量。在Perl里,它们的命名方式像$1或$2。模式里的括号有多少对,匹配变量就有多少个。
#!/usr/bin/perl -w
$_ = "Hello there,neighbor";
if (/s(w+),/) {
print "the word was $1n";
}
#!/usr/bin/perl -w
$_ = "Hello there, neighbor";
if (/(S+) (S+), (S+)/) {
print "words were $1 $2 $3n";
}
自动匹配变量:$&、$`、$’,字符串里实际匹配模式的部分会被自动存进$&里。
#!/usr/bin/perl -w
if ("Hello there, neighbor" =~ /s(w+),/) {
print "That actually matched '$&'.n"; # That actually matched ' there,'.
print "That was ($`)($&)($').n"; # That was (Hello)( there,)( neighbor).
}
第一份记忆存储在$1,但$&里匹配到的是整个段落。相符段落的前置字符串会存到$`里,而后置字符串则存在$’里。
匹配变量(包括自动匹配变量和有编号的匹配变量)最常用在替换运算中。
通用量词:
模式/a{5,15}/可匹配重复出现5到15次的字母a;如果省略第二个数字(但包含逗号),则表示没有上限,如:/(fred){3,}/;还可以是固定次数,如:/w{8}/
量词*、+、?是简写:星号和量词{0}相同,加号相当于{1},问号也可以写成{0,1}。
4)优先级
最高等级是括号(),用于划分组以及存储;第二个等级是量词,也就是反复操作符(包括星号、加号、问号和花括号量词);第三级是锚点与序列(包括^、$、b、B);最低以及是分隔用的竖线(|)。
优先级的范例:/^fred|barney$/与/^(fred|barney)$/;/^(w+)s+(w+)$/
5)模式测试程序
下面是一个有用的程序,可以用来检测某些字符串是否匹配所指定的模式:
#!/usr/bin/perl -w
while (<>) {
chomp;
if (/YOUR_PATTERN_GOES_HERE/) {
print "Matched:|$`<$&>$'|n";
} else {
print "No matched : |$_|n";
}
}
假设你所使用的模式是/match/,所输入的字符串是beforematchafter,结果为Matched:|before
习题:
1)#!/usr/bin/perl -w
while (<>) {
chomp;
if (/match/) {
print "Matched:|$`<$&>$'|n";
} else {
print "No matched : |$_|n";
}
}
2)#!/usr/bin/perl -w
while (<>) {
chomp;
if (/(w+)a$/) {
print "Matched:|$`<$&>$'|n";
} else {
print "No matched : |$_|n";
}
}
参考答案如下:
#!/usr/bin/perl -w
while (<>) {
chomp;
if (/ab/) {
print "Matched:|$`<$&>$'|n";
} else {
print "No matched : |$_|n";
}
}
3)#!/usr/bin/perl -w
while (<>) {
chomp;
if (/(w+)a$/) {
print "The result: '$_'n";
}
}
参考答案如下:
#!/usr/bin/perl -w
while (
chomp;
if (/(bw*ab)/) {
print "Matched:|$`<$&>$'|n";
print "$1 contains '$1'n";
} else {
print "No matched : |$_|n";
}
}
5) #!/usr/bin/perl -w
while (<>) {
chomp;
if (/(s+)$/) {
print "'$`<$&>'n";
}
}
参考答案如下:
#!/usr/bin/perl -w
while (<>) {
chomp;
if (/s+$/) {
print "$_#n";
}
}