perl入門的基本知識點

來源:互聯網
上載者:User

 我發下個人的連結:http://www.wachnew.com

 

以下是我學perl的時候記錄的一些知識點,方便以後查看,以下代碼是一個程式檔案,可以在perl解析器上成功運行。

 

#!/usr/bin/perl -w

 

#知識點1:s/匹配模式串/替換串/;
# s///替換成功後返回為真,否則為假
$_ = "hello world, again..";
s/hello/nice/;   #用nice把hello替換掉
print "$_/n";

s//s(/w+)/ a man's $1/;
print "$_/n";

$_ = "green scaly dinosaur";
s/(?<name1>/w+) (?<name2>/w+)/$+{name2},$+{name1}/; #用雜湊值來捕捉,然後將green和scaly位置替換。
print "$_/n";  #變為scaly,green dinosaur

#在開始位置處替換
s/^/huge, /;   #變為huge, scaly,green dinosaur
print "$_/n";

s/,.*reen//;#空替換,變為huge dinosaur
print "$_/n";

s/green/red/; #匹配失敗,仍為huge dinosaur
print "$_/n";

 

#知識點2:s///g,加上g後表示全域替換,如下例子:
$_ = "home, sweet home";
s/home/cave/g;#加上g變成全域
print "$_/n";   #變為cave, sweet cave

#將任意連續的空白縮減為單一空白
$_ = "   you    are        right,are      you!  !     ";
s//s+/ /g;#加上g變成全域
print "$_/n";

#刪除開頭的結尾的空白
s/^/s+|/s+$//g; #將開頭和結尾的空白刪除
print "$_/n";

 

#知識點3:替換的不同定界符有:s///,s###等,或s[]{},s{}[],s(){},s{}()等

 

#知識點4:大小寫轉換
#1: /U後能轉成大寫
#2: /L後能轉成小寫
#3: /E後結束大小寫轉換影響
#4: 使用小寫形式(/l與/u)時隻影響其後面的第一個字元
#5:同時使用/u/L將第一個轉為大寫,其後都轉成小寫
#6:同時使用/l/U將第一個轉為小寫,其後都轉成大寫
$_ = "I saw Barney with Fred.";
s/(fred|barney)//U$1/gi; #將忽略大小寫fred和barney全域替換成大寫的FRED和BARNEY,即I saw BARNEY with FRED.
print "$_/n";
s/(fred|barney)//L$1/gi; #同是,將fred和barney全域的替換成小寫,即I saw barney with fred
print "$_/n";
s/(/w+) with (/w+)//U$2/E with $1/gi; #將fred和barney替換位置,但是第一個為大寫後面的全小寫。
print "$_/n"; #即為I saw FRED with barney
s/(fred|barney)//u$1/gi; #將fred或barney的第一個字母變為大寫,其餘不變
print "$_/n"; #即為I saw FRED with Barney
s/(fred|brney)//u/L$1/gi; #將fred和barney的第一個字母變大寫,其餘字母變小寫
print "$_/n";#即為I saw Fred with Barney

 

#知識點5:split操作符(根據分隔字元分隔成各個子串並儲存在列表中),如下:
my @list = split /:/, "a:b:c";
print "there are:@list/n";  #得到"a","b","c"
@list = split /;/, "abc;ef;;g;h";
print "There are :@list/n";#得到"abc","ef","","g","h"

 

#知識點6:join函數,不使用模式,其功能與split相反,如下
#函數用法:join $jiao_shui,@sub_string
#子串列表必須至少有2個元素,否則膠水塗不進去。
my $_ = join ":", @list;#join的第一個參數可以理解為膠水,將它與後面列表的每個元素一一串連。
print "$_/n";

#下面是join和split合作的例子,先將某字串根據分隔字元拆分,然後用另一分隔字元串連
@list = split /:/, $_;
$_ = join "-", @list;
print "$_/n";

 

#知識點7:列表內容相關的m//
$_ = "hello there, neighbor!";
my($first,$second,$third) = m/(/S+) (/S+), (/S+)/;
print "$first, $second, $third/n";

my $text = "Fred dropped a 5 ton granite block on M4. Slate";
my @words = ($text =~ m/([a-z]+)/gi); #先綁定$test作為匹配的對象,然後匹配的結果存入列表@words中
print "there is :@words/n";

 

#知識點8:注意非貪婪量詞 +? *? ??  {5,10}? {8,}?等

#知識點9:跨行的模式比對:加上模式比對修飾符/m(理解為multiple lines)
$_ = "I'm much better/nthan Barney is/nat bowling,/nWilma./n";
print "Found 'wilma' at start of line/n" if /^wilma/b/im;

#知識點10:字串處理搜尋index($source, $sub_string, $No),其含義是需要在$source串中尋找到第一次出現子串$sub_string,
#如果找到,則返回具體位置,否則返回-1,$No表示搜尋的起始位置,如果第三個參數$No省略,則從起始位置0處開始搜尋
#rindex($source, $sub_string, $No):與index基本相同,但是他表示從最後向前尋找,並且$No表示限定返回的最大位置。
my $stuff = "howdy world!";
my $where1 = index($stuff, "w");  #$where1 = 2
my $where2 = index($stuff, "w", $where1+1);  #$where2 = 6
my $where3 = index($stuff, "w", $where2+1);  #未找到,返回-1
print "$where1, $where2, $where3/n";

my $fred = "Yabba dabba doo!";
$where1 = rindex($fred,"abba");   #$where1 =7
$where2 = rindex($fred,"abba", $where1 - 1); #$where2 = 1
$where3 = rindex($fred,"abba", $where2 - 1); #$where3 = -1
print "$where1, $where2, $where3/n";

 

#知識點11: 用substr處理子串
#格式: $part = substr($string, $initial_position, $length);
#參數: $string:要處理的字串
#      $initial_position: $string中的起始位置
#      $length:  從起始位置起計算得到的總的子串長度
#傳回值: 得到子串存入$part.

 

my $mineral = substr("Fred J. Flintstone", 8, 5); #值為"Flint"
my $rock = substr "Fred J. Flintstone",13,1000; #值為"stone",雖然得不到長度1000的字元
my $out = substr("some very long string", -3, 2); #$out = "in", 最後位置為-1,所以-3位置是"i"
print "mineral = $mineral, rock = $rock, out = $out/n";

my $long = "some very very long string";
my $right = substr($long, index($long, "l") ); #得到long string
print "$right/n";

my $string = "Hello, world!";
substr($string, 0, 5) = "Goodbye";  #$string現為:Goodbye, world!
print "$string/n";

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.