perl-正则表达式处理文本
1.用s///替换
此操作可将指定变量合乎模式的部分替换为另一个字符串。
$_ = "He's out bowling with Barney tonight.";
if ( s/Barney/Fred/){
print "$_";
}
输出:
He's out bowling with Fred tonight.
当然模式串和替换串还可以更复杂:
$_ = "He's out bowling with Barney tonight.";
s/with (\w+)/against $1's team/){
print "$_";
}
输出:
He's out bowling against Barney's team tonight.
$_ = "green scaly dinosaur";
if (s/(\w+) (\w+)/$1, $2/){
print "The string change into : $_\n";
}
if ( s/^/huge, /){
print "The string change into : $_\n";
}
if (s/, .*een,/ /){
print "The string change into : $_\n";
}
输出:
The string change into : green, scaly dinosaur
The string change into : huge, green, scaly dinosaur
The string change into : huge scaly dinosaur
2.用/g进行全局替换
在前面的例子中可以看到,即使有其他可以替换的部分,s///也只会进行一次替换。当然,这只是默认的行为而已。/g可以让s///进行所有的可能的、不重复的替换。
$_ = "home, sweet home!";
if ( s/home/cave/g){
print "$_";
}
输出:cave, sweet cave!
常用的全局替换是缩减空白,也就是将连续的空白转换为单一空白:
$_= "Input data and output data";
if (s/\s+/ /g){
print "$_";
}
输出:
Input data and output data
如何删除开头和结尾的空白,其实很简单:
s/^\s+//g ;#删除开头的空白
s/\s+$//g ;#删除结尾的空白
3.大小写转换
在替换运算中,常常需要把单词全部改写长大写字母(或小写字母)。用PERL很容易做到,只要使用某些反斜线转移字符就行了。
\U 移字符将其后的字符转换为大写
\L 移字符将其后的字符转换为小写
使用小写字母\u和 \l 他们只会影响之后的第一个字符。
$_="I saw Barney with Fred.";
if (s/(fred|barney)/\U$1/gi){
print "$_\n";
}
if (s/(fred|barney)/\L$1/gi){
print "$_\n";
}
if (s/(fred|barney)/\u$1/gi){
print "$_\n";
}
if (s/(fred|barney)/\l$1/gi){
print "$_\n";
}
输出:
I saw BARNEY with FRED.
I saw barney with fred.
I saw Barney with Fred.
I saw barney with fred.