1. 題目
2. 代碼與輸出
1 #-----------------------------------------------------------#
2 # Source: Learning Perl, chapter8,exercise-1
3 # Date: 2012-01-18
4 # Author: xiaodongrush
5 #-----------------------------------------------------------#
6 use 5.010;
7 while(<>) {
8 chomp;
9 if($_ =~ m|match|) {
10 say "<$`><$&><$'>";
11 }
12 }
13 #-----------------------------------------------------------#
14 # 1. /match/ 是對 m/match/的簡寫
15 # m/match/,m|match|,m<match>,m{match}這些都是等價的
16 # 2. =~可以匹配指定的變數
17 #-----------------------------------------------------------#
1 #-----------------------------------------------------------#
2 # Source: Learning Perl, chapter8,exercise-2
3 # Date: 2012-01-18
4 # Author: xiaodongrush
5 #-----------------------------------------------------------#
6 use 5.010;
7 while(<>) {
8 chomp;
9 if($_ =~ m|\b\w*a\b|) {
10 say "<$`><$&><$'>";
11 }
12 }
13 #-----------------------------------------------------------#
14 # 1. \w等價於[A-Za-z0-9_],即字母、數字和底線
15 # 2. \b是表示單詞開始或結束位置
16 # 3. 不能匹配到Mrs._Wilma_Flintsone,因為底線屬於\w,為\b
17 # 匹配“非\w”的位置
18 #-----------------------------------------------------------#
1 #-----------------------------------------------------------#
2 # Source: Learning Perl, chapter8,exercise-3
3 # Date: 2012-01-18
4 # Author: xiaodongrush
5 #-----------------------------------------------------------#
6 use 5.010;
7 while(<>) {
8 chomp;
9 if($_ =~ m|\b(\w*a)\b|) {
10 say "<$`><$&><$'>";
11 say '$1 contains ' . "\'$1\'";
12 }
13 }
14 #-----------------------------------------------------------#
1 #-----------------------------------------------------------#
2 # Source: Learning Perl, chapter8,exercise-4
3 # Date: 2012-01-18
4 # Author: xiaodongrush
5 #-----------------------------------------------------------#
6 use 5.010;
7 while(<>) {
8 chomp;
9 if($_ =~ m|\b(?<word>\w*a)\b|) {
10 say "<$`><$&><$'>";
11 say 'word contains ' . "\'$+{word}\'";
12 }
13 }
14 #-----------------------------------------------------------#
15 # 1. 定義命名捕獲 (?<命名>待捕獲模式串)
16 # 使用命名捕獲 $+{命名}
17 #-----------------------------------------------------------#
1 #-----------------------------------------------------------#
2 # Source: Learning Perl, chapter8,exercise-5
3 # Date: 2012-01-18
4 # Author: xiaodongrush
5 #-----------------------------------------------------------#
6 use 5.010;
7 while(<>) {
8 chomp;
9 if($_ =~ m|\b\w*a\b(?<word>.{0,5})|) {
10 say "<$`><$&><$'>";
11 say '$1 contains ' . "\'$1\'";
12 say 'word contains ' . "\'$+{word}\'";
13 }
14 }
15 #-----------------------------------------------------------#
16 # 1. 通用量詞,{0,5} 0次到5次,{0,}0次或者以上
17 #-----------------------------------------------------------#
1 #-----------------------------------------------------------#
2 # Source: Learning Perl, chapter8,exercise-6
3 # Date: 2012-01-18
4 # Author: xiaodongrush
5 #-----------------------------------------------------------#
6 use 5.010;
7 while(<>) {
8 chomp;
9 if($_ =~ m|\s$|) {
10 say "|$`|$&|$'|";
11 say "|$_|";
12 }
13 }
14 #-----------------------------------------------------------#
15 # 如果匹配成功,那麼$&將是字串的最後一個空白,而$'是空串
16 # $`是除了最後一個空白的前面所有字元
17 #-----------------------------------------------------------#
3. 檔案
/Files/pangxiaodong/LearningPerl/ch8-answer.rar