Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1065908
  • 博文数量: 239
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 3618
  • 用 户 组: 普通用户
  • 注册时间: 2012-11-12 13:17
文章分类

全部博文(239)

文章存档

2021年(1)

2016年(1)

2015年(30)

2014年(91)

2013年(116)

分类: LINUX

2013-08-17 20:38:49

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) |
给主人留下些什么吧!~~