1. Question
2. Code and Output
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/is short for M/match/
15 # m/match/, m | match |, m <match>, m {match} are equivalent.
16 #2. = ~ Can match the specified variable
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 is equivalent to [A-Za-z0-9 _], that is, letters, numbers, and underscores
15 #2. \ B indicates the start or end position of a word
16 #3. It cannot match mrs. _ wilma_flintsone because the underline belongs to \ W and is \ B.
17 # match the position of "Non \ 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 ins'. "\ '$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. Define name capture (? <Name> mode string to be captured)
16 # capture with name $ + {name}
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 ins'. "\ '$1 \'";
12 say 'word contains'. "\ '$ + {word }\'";
13}
14}
15 #-----------------------------------------------------------#
16 #1. General quantifiers, {0, 5} 0 to 5 times, {0,} 0 times or more
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 # If the match is successful, $ & will be the last blank space of the string, and $ 'will be an empty string
16 # $ 'is all the characters except the last blank
17 #-----------------------------------------------------------#
3. File
/Files/pangxiaodong/learns ningperl/ch8-answer.rar