perl学习:一些字符串操作

1.编码转换

  use Text::Iconv;
  $converter = Text::Iconv->new("fromcode", "tocode");

  $converted = $converter->convert("Text to convert"); 

 可能需要安装Text::Iconv模块,

 cpan命令

 install  Text::Iconv

 

2. 获取指定格式的日期

my $date = strftime "%Y-%m-%d %H:%M:%S", localtime;

 

3.字符串分割并存入数组

 #将$receivers以;为分隔符分割并存入wwids中

my @wwids = split /;/,$receivers;

4.字符串匹配

/i 大小写忽略

/s 匹配任意字符

/x 运行在正则表达式中加入空白

^表示前锚位

&表示后锚位

\b表示单词边界

 =~ 绑定操作符,右边模式匹配左边字符串

 

 5.截取字符

 正则表达式

/\s(\w+)\s/ 扩后内是需要获取的内容,该表达式表示截取两个空格之间的单词

如 $a="this is a word";

if($a =~  /\s(\w+)\s/ )

{

    print "$1 ,$2"; 

  

6.字符串替换

 s/oldword/newword/ 是基本命令

 s/oldword/newword/g 全局替换

 

 

 

 

posted on 2012-05-28 22:20  缠中说禅  阅读(457)  评论(0编辑  收藏  举报