Perl-Chapter 8-regular expression-Exercises

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.