我发下个人的链接:http://www.wachnew.com
以下是我学perl的时候记录的一些知识点,方便以后查看,以下代码是一个程序文件,可以在perl解析器上成功运行。
#!/usr/bin/perl -w
#知识点1:s/匹配模式串/替换串/;
# s///替换成功后返回为真,否则为假
$_ = "hello world, again..";
s/hello/nice/; #用nice把hello替换掉
print "$_/n";
s//s(/w+)/ a man's $1/;
print "$_/n";
$_ = "green scaly dinosaur";
s/(?<name1>/w+) (?<name2>/w+)/$+{name2},$+{name1}/; #用哈希值来捕捉,然后将green和scaly位置替换。
print "$_/n"; #变为scaly,green dinosaur
#在开始位置处替换
s/^/huge, /; #变为huge, scaly,green dinosaur
print "$_/n";
s/,.*reen//;#空替换,变为huge dinosaur
print "$_/n";
s/green/red/; #匹配失败,仍为huge dinosaur
print "$_/n";
#知识点2:s///g,加上g后表示全局替换,如下例子:
$_ = "home, sweet home";
s/home/cave/g;#加上g变成全局
print "$_/n"; #变为cave, sweet cave
#将任意连续的空白缩减为单一空白
$_ = " you are right,are you! ! ";
s//s+/ /g;#加上g变成全局
print "$_/n";
#删除开头的结尾的空白
s/^/s+|/s+$//g; #将开头和结尾的空白删除
print "$_/n";
#知识点3:替换的不同定界符有:s///,s###等,或s[]{},s{}[],s(){},s{}()等
#知识点4:大小写转换
#1: /U后能转成大写
#2: /L后能转成小写
#3: /E后结束大小写转换影响
#4: 使用小写形式(/l与/u)时只影响其后面的第一个字符
#5:同时使用/u/L将第一个转为大写,其后都转成小写
#6:同时使用/l/U将第一个转为小写,其后都转成大写
$_ = "I saw Barney with Fred.";
s/(fred|barney)//U$1/gi; #将忽略大小写的fred和barney全局替换成大写的FRED和BARNEY,即I saw BARNEY with FRED.
print "$_/n";
s/(fred|barney)//L$1/gi; #同是,将fred和barney全局的替换成小写,即I saw barney with fred
print "$_/n";
s/(/w+) with (/w+)//U$2/E with $1/gi; #将fred和barney替换位置,但是第一个为大写后面的全小写。
print "$_/n"; #即为I saw FRED with barney
s/(fred|barney)//u$1/gi; #将fred或barney的第一个字母变为大写,其余不变
print "$_/n"; #即为I saw FRED with Barney
s/(fred|brney)//u/L$1/gi; #将fred和barney的第一个字母变大写,其余字母变小写
print "$_/n";#即为I saw Fred with Barney
#知识点5:split操作符(根据分隔符分隔成各个子串并保存在列表中),如下:
my @list = split /:/, "a:b:c";
print "there are:@list/n"; #得到"a","b","c"
@list = split /;/, "abc;ef;;g;h";
print "There are :@list/n";#得到"abc","ef","","g","h"
#知识点6:join函数,不使用模式,其功能与split相反,如下
#函数用法:join $jiao_shui,@sub_string
#子串列表必须至少有2个元素,否则胶水涂不进去。
my $_ = join ":", @list;#join的第一个参数可以理解为胶水,将它与后面列表的每个元素一一连接。
print "$_/n";
#下面是join和split合作的例子,先将某字符串根据分隔符拆分,然后用另一分隔符连接
@list = split /:/, $_;
$_ = join "-", @list;
print "$_/n";
#知识点7:列表上下文的m//
$_ = "hello there, neighbor!";
my($first,$second,$third) = m/(/S+) (/S+), (/S+)/;
print "$first, $second, $third/n";
my $text = "Fred dropped a 5 ton granite block on M4. Slate";
my @words = ($text =~ m/([a-z]+)/gi); #先绑定$test作为匹配的对象,然后匹配的结果存入列表@words中
print "there is :@words/n";
#知识点8:注意非贪婪量词 +? *? ?? {5,10}? {8,}?等
#知识点9:跨行的模式匹配:加上模式匹配修饰符/m(理解为multiple lines)
$_ = "I'm much better/nthan Barney is/nat bowling,/nWilma./n";
print "Found 'wilma' at start of line/n" if /^wilma/b/im;
#知识点10:字符串处理搜索index($source, $sub_string, $No),其含义是需要在$source串中寻找到第一次出现子串$sub_string,
#如果找到,则返回具体位置,否则返回-1,$No表示搜索的起始位置,如果第三个参数$No省略,则从起始位置0处开始搜索
#rindex($source, $sub_string, $No):与index基本相同,但是他表示从最后向前查找,并且$No表示限定返回的最大位置。
my $stuff = "howdy world!";
my $where1 = index($stuff, "w"); #$where1 = 2
my $where2 = index($stuff, "w", $where1+1); #$where2 = 6
my $where3 = index($stuff, "w", $where2+1); #未找到,返回-1
print "$where1, $where2, $where3/n";
my $fred = "Yabba dabba doo!";
$where1 = rindex($fred,"abba"); #$where1 =7
$where2 = rindex($fred,"abba", $where1 - 1); #$where2 = 1
$where3 = rindex($fred,"abba", $where2 - 1); #$where3 = -1
print "$where1, $where2, $where3/n";
#知识点11: 用substr处理子串
#格式: $part = substr($string, $initial_position, $length);
#参数: $string:要处理的字符串
# $initial_position: $string中的起始位置
# $length: 从起始位置起计算得到的总的子串长度
#返回值: 得到子串存入$part.
my $mineral = substr("Fred J. Flintstone", 8, 5); #值为"Flint"
my $rock = substr "Fred J. Flintstone",13,1000; #值为"stone",虽然得不到长度1000的字符
my $out = substr("some very long string", -3, 2); #$out = "in", 最后位置为-1,所以-3位置是"i"
print "mineral = $mineral, rock = $rock, out = $out/n";
my $long = "some very very long string";
my $right = substr($long, index($long, "l") ); #得到long string
print "$right/n";
my $string = "Hello, world!";
substr($string, 0, 5) = "Goodbye"; #$string现为:Goodbye, world!
print "$string/n";