1.用s///替换
[root@oa1 home]# cat test1
#!/usr/bin/perl
$_ = "He's out bowling with Barney tonight.";
s/Barney/Fred/;
print "$_\n";
[root@oa1 home]# ./test1
He's out bowling with Fred tonight.
2.用$1替换变量
[root@oa1 home]# cat test2
#!/usr/bin/perl
$_ = "He's out bowling with Barney tonight.";
s/with (\w+)/against $1's team/;
print "$_\n";
[root@oa1 home]# ./test2
He's out bowling against Barney's team tonight.
3.
$_ = "greem scaly dinosaur";
s/(\w+) (\w+) /$2, $1/; #替换后为"scaly, green dinosaur"
s/^/huge, /; #替换后为"huge, scaly,green dinosaur"
s/,.*een//; #空替换,此时为"huge dinosaur"
s/green/red/; #匹配失败:仍为"huge dinosaur"
s/\w+$/($`!)$&/; #替换后为 "huge (huge!) dinosaur"
s/s+(!\W+)/$1 /; #替换后为"huge (huge!) dinosaur"
s/huge/gigantic/; #替换后为"gigantic (huge!) dinosaur"
$_ = "fred filintstone";
if (s/fred/willma){
print "Successfully replaced fred with wilma!\n";
}
阅读(1846) | 评论(0) | 转发(0) |