您的位置:首页 > 其它

学习笔记-Perl-记录最简单的一些实例-2

2019-08-16 14:26 204 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。 本文链接:https://blog.csdn.net/smallfishy/article/details/99677337

#Example11     #哈希数列2
#!/usr/bin/perl

my %hash = ("a" => 1, "b" => 2, "c" =>3);
my @k = keys %hash;
my @v = values %hash;
print "@k\n";
print "@v\n";

while ( ($key, $value) = each %hash ) {
  print "$key => $value\n";

foreach $key (sort keys %hash) {
   $value = $hash{$key};
   print "$key => $value\n";
   
}

#-----------------------------------------------------------------------------------------------------------------------------------------------------
#Example12 正则表达式:$_的简写
#!/usr/bin/perl

$_ = "yu qq QQ doo";
if (/a/) {
   print "It has matchd!\n";
}
else {
   print "It has not matchd!\n";
}

#-----------------------------------------------------------------------------------------------------------------------------------------------------
#Example13     #正则表达式:(.)\1 匹配连续出现的两个相同的字符 
#!/usr/bin/perl                  

$_ = "yu qq QQ oo";
if (/(.)\1/) {   #将会匹配qq、QQ、oo 
                 #'\1'表示刚刚匹配过的字符 
   print "It matchd same character next to itself!\n";
}

$_ = "yabba dabba doo";
if (/y(....) d\1/) {   #'\1'表示刚刚匹配过的字符,即(....)匹配到的东西 
   print "It matched the same after y and d!\n";
}

$_ = "yabba dabba doo";      
if (/y(.)(.)\2\1/) {    #将会匹配abba
   print "It matched the same after y and d!\n";
}

#-----------------------------------------------------------------------------------------------------------------------------------------------------
#Example14     #正则表达式:\s 匹配换页、制表、换行、回车、空格 
                          #\w 匹配字母数字下划线
                          #\d 匹配数字  ...
#!/usr/bin/perl 

$_ = "Hello there, neighor";
if (/\s(\w+),/) {         #匹配规则,先是一个空格,然后是一个单词,然后是一个","
 print "The word was $1\n";   # $1:第一个括号匹配到的内容
}

print "$_\n";
if (/(\S+) (\S+), (\S+)/) {   # $i:第i个括号匹配到的内容
print "words were $1 $3 $2\n";
}

my $dino = "I fear that I'll be extinct after 10000 years.";
if ($dino =~ /(\d*) years) {  #绑定操作符,用右边的匹配左边的字符串或变量
print "That said '$1' years.\n";   #捕捉数字
}

$_ = "Hello there, neighor";
if (/(there)?, (nei|ber)/) {   #匹配串是'there, nei'或者是'there, ber',匹配到了第一个
 print "The word was $1\n";    #于是输出第一个捕捉串there
 print "The word was $2\n";    #输出第二个括号中的部分nei
}

$_ = "Hello there, neighor";
if (/(there)*, (nei|ber)/) {   #这里用*也是可以的
 print "The word was $1\n";
 print "The word was $2\n";
}

if (/(there)?there, (nei|ber)/) {   #匹配串是'there, nei'或者是'there, ber',匹配到了第一个
 print "The word was $1\n";         #但是匹配到的there是括号外面的there,并不是第一个括号中的there
 print "The word was $2\n";         #输出第二个括号中的nei
}

if (/(?:there)?(nei|ber)/) {    #匹配串是'there nei'或者是'there ber',匹配到了第一个
 print "The word was $1\n";     #但是there使用了不引用修饰符,于是$1指代的就是nei
}

$_ = "Hello there, neighor";
if (/\s(\w+),/) {               #匹配规则,先是一个空格,然后是一个单词,然后是一个","
 print "The matched word was $&\n";   #在次引用了自动匹配变量$&,指代匹配到的内容
 print "The character before matched was $`\n";  #在次引用了自动匹配变量$&,指代匹配到内容之前的字符
 print "The character after matched was $'\n";   #在次引用了自动匹配变量$&,指代匹配到内容之后的字符
}

$_ = "Hello there, neighor";
if (/(\w{3,}),/) {             #用数字指定匹配3个以上的字符,然后紧跟着还要有个','
   print "The matched word was $&\n";   #在此可以看到输出部分比着上面的少了一个空格
}

#-----------------------------------------------------------------------------------------------------------------------------------------------------
#Example15          #检验一下模式匹配     
#!/usr/bin/perl

while (<>){  #接收键盘输入
chomp;       #删除最后的回车
if (/yu/) {  #模式匹配字符yu     
   print "The matched word was |$`<$&>$'|\n";   #提示匹配结果
}
else { 
   print "No matched: |$_|\n";    #匹配失败,输出 键盘输入的字符
}
}

#-----------------------------------------------------------------------------------------------------------------------------------------------------
#Example17             #用 s/// 替换
#!/usr/bin/perl

$_ = "He's out bowling with Barney tonight.";
$s=s/Barney/Fred/;
print "$_\n";          #输出替换后的字符串
print "$s\n";          #输出替换命令执行的结果:1 ,若替换失败,返回空值

$_ = "green scaly dinosaur";
print "$_\n";          #green scaly dinosaur
s/(\w+) (\w+)/$2, $1/; #将第一个,第二个单词调换一下位置
print "$_\n";          #scaly, green dinosaur
s/^/huge, /;           #行首加上'huge, '
print "$_\n";          #huge, scaly, green dinosaur
s/,.*een//;            #匹配第一个','到'een'之间所有字符,并删掉
print "$_\n";          #huge dinosaur
s/green/red/;          #green已经没有了,上面也不做
print "$_\n";          #huge dinosaur
s/\w+$/($`!)$&/;       #先找到最后一个单词,然后在他($&)前面插入该词之前的部分($`)和一个'!'
                       #也就是替换为 该词之前的部分($`)和一个'!'该词 <=> ($`!)$&
print "$_\n";          #huge (huge !)dinosaur
s/\s+(!\W+)/$1 /;      #匹配 一个空格、叹号、非字符,然后替换成 叹号、非字符、空格 
print "$_\n";          #huge (huge!) dinosaur
s/huge/gigantic/;      #替换单词huge(前后不能有字符)
print "$_\n";          #gigantic (huge!) dinosaur

#-----------------------------------------------------------------------------------------------------------------------------------------------------
#Example18             # s/// 的定界符'/'的不同写法
#!/usr/bin/perl

$_ = "He's out bowling with Barney tonight.";
s/Barney/Fred/;        #规范写法
print "$_\n";

s#Fred#Barney#;        #可以用'#'替换'/'
print "$_\n";

s{Barney}{Fred};       #也可以不使用定界符,而是用成对出现的符号
print "$_\n";

s(Fred){Barney};       #成对出现的符号只需要成对出现,两对之间没有关联
print "$_\n";

#-----------------------------------------------------------------------------------------------------------------------------------------------------
#Example19             #可选修饰符 /g 全局匹配 /i 忽略大小写 /s 任意字符 /x 加入空白
#!/usr/bin/perl        #单词锚位 /b 单词边界锚位 ^(脱字符) 行首 $(美元符号) 行尾

$_ = "He's out bowling with Barney tonight.";
print "$_\n"; 
s#o#n#g;               #全局匹配,将所有的'o'换成'n'
print "$_\n";
s#he#We#gi;            #全局匹配,将所有的'he'换成'We',并且忽略大小写(只忽略he的大小写)
print "$_\n";

s#(bnwling|Barney)#\L$1#gi; #忽略大小写全局匹配,将Barney或bnwling转换称小写
print "$_\n";                          
s#(Barney|bnwling)#\U$1#gi; #忽略大小写全局匹配,将Barney或bnwling转换称大写
print "$_\n";

s#(bnwling|Barney|we)#\l$1#gi;  #忽略大小写全局匹配,将Barney或bnwling或we的首字母转换称大写
print "$_\n";
s#(we)#\u$1#gi;    #忽略大小写全局匹配,将we的首字母转换称大写
print "$_\n";

#-----------------------------------------------------------------------------------------------------------------------------------------------------
#Example19         #split函数:以指定分隔符切割字符串
#!/usr/bin/perl

$_ = "He's out bowling with Barney tonight.";
print "$_\n";
@ff = split ;      #以默认分隔符(空格)切割字符串$_,并将结果保存到@ff中
print "@ff\n";
print @ff;

@ff = split /:/, "He's :abc :xxd";   #得到("He's ","abc ","xxd")
print "@ff\n";     #以':'为分隔符切割字符串"He's :abc :xxd",并将结果保存到@ff中
print @ff;

@ff = split /\s+/, "  He's :abc :xxd"; #得到("He's",":abc",":xxd")
print "@ff\n";     #以空白符为分隔符切割字符串"  He's :abc :xxd",并将结果保存到@ff中
print @ff;         #所有的空白(空格、制表符等)都被当做一个空格来处理,会自动舍弃前面的空白

@ff = split /:/, ":::He's::xxd:abc:dd::";  #得到的是(“ ”,“ ”,“He's”,“ ”,“xxd”,“abc”,“dd”)
print "@ff\n";     #保留前面和中间的空字符(串),自动舍弃后面的空字符(串)           
print @ff;

@ff = split /:/, ":::He's::xxd:abc:dd::";
print "@ff\n";     #得到"   He's  xxd abc dd"
@ff = split /\s+/, "@ff";   #若想要处理字符串,需要加上引号
print "@ff\n";     #得到" He's xxd abc dd"

@ff = split /:/, ":::He's::xxd:abc:dd::";
print "@ff\n";     #得到"   He's  xxd abc dd"
@ff = split /\s+/, @ff;     #若想要处理字符串,需要加上引号,否则处理的字符长度
print "@ff\n";     #得到8

#-----------------------------------------------------------------------------------------------------------------------------------------------------
#Example20         #join:用指定的分隔符将若干个字符(串)连接成一个字符串
#!/usr/bin/perl    #和split刚好相反

my $x = join ":", 1, 2, 4, 6, 8;  #得到"1:2:4:6:8"
print "$x\n";
print $x;
my @a = split /:/, $x;            #得到"1 2 4 6 8"
print "@a\n";
my $x = join "-", @a;             #得到"1-2-4-6-8"
print "$x\n";

while(<>){  #接收键盘的任意输入
chomp;      #过滤最后的回车
print join("\t",(split /:/)), "\n";  #先以':'为分隔符分隔输入,再用'\t'连接字符
}
 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: