代码:
#! /usr/bin/perl
open(fileIN,"<tt.txt")||dIE"can not open the file: $!";
open(fileOUT,">tt_out.txt");
@fileList=<fileIN>; #add the file content to @fileList.
#print $fileList[2],"\n";
foreach(@fileList)
{
@strList=split(/\s+/,$_);
if($_=~/$strList[1]/)
{
$strList_upper=uc $strList[1];
#print "$`$strList_upper$'";
print fileOUT "$`$strList_upper$'";
}
}
实例:
tt.txt:
// hello heloworld ilove
// world lowcase tp uppercase
// n12ll_ckt_rf low case
run the perl file.
tt_out.txt:
// HELLO heloworld ilove
// WORLD lowcase tp uppercase
// N12LL_CKT_RF low case
呵呵,这是帮一个同事写的,感觉这个perl对我来说有两点:
一点是:
if($_=~/$strList[1]/)
就是说直接获得的变量直接匹配。
二点是:
print fileOUT "$`$strList_upper$'"; $` and $' 可以匹配匹配字前后的内容,这个是我以前不知道的。
总结以上是内存溢出为你收集整理的perl将一段文字的第二个字符串由小写转化为大写,其他不变全部内容,希望文章能够帮你解决perl将一段文字的第二个字符串由小写转化为大写,其他不变所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)