perl學習 一些字串操作

2022-03-30 04:32:11 字數 789 閱讀 5978

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 全域性替換

字串的一些操作

一,把字串的首字母大寫返回乙個新的字串 1.1簡單寫法,把乙個單詞的首字母大寫 string.prototype.firstuppercase function 1.2字串中所有單詞首字母大寫,非首字母小寫 string.prototype.firstuppercase function 另一種寫法...

perl 字串操作

字串陣列元素賦值 tmp qw aaa bbb kkk 9000 相當於 tmp aaa bbb kkk 9000 字串比較,絕不能用 要用eq macg localhost perltest vi tip.pl usr bin perl print input while chomp input ...

11 10學習筆記 引用 一些字串操作

print assignment mylist shoplist 我購買了第乙個專案,所以我把它從列表中刪除 del shoplist 0 print shoplist is shoplist print mylist is mylist print copy by a full slice myl...