Perl-Chapter 7-roaming Regular Expression kingdom-Exercises

Source: Internet
Author: User

1. Question

There are some problems described in the original question. Question 1st: match the row containing fred. Question 2nd: match the rows containing fred or Fred. Question 3rd: match the row containing the vertex number. Question 4th: match the rows starting with an upper-case letter. Question 5th: contains two consecutive rows with the same non-space characters, such as AA, aa, ¥, and so on. Question 6th: Match rows with wilma and fred.

2. Code and Output

Question 1st: match the row containing fred.

1 #-----------------------------------------------------------#
2 # Source: Learning Perl, chapter7, exercise-1
3 # Date: 2012-01-17
4 # Author: xiaodongrush
5 #-----------------------------------------------------------#
6 use 5.010;
7 while (<> ){
8 if (/fred /){
9 chomp;
10 say $ _;
11}
12}
13 #-----------------------------------------------------------#
14 #/fred/match a string containing fred
15 #/^ fred $/match strings that contain only fred
16 #-----------------------------------------------------------#

Question 2nd: match the rows containing fred or Fred.

1 #-----------------------------------------------------------#
2 # Source: Learning Perl, chapter7, exercise-2
3 # Date: 2012-01-17
4 # Author: xiaodongrush
5 #-----------------------------------------------------------#
6 use 5.010;
7 while (<> ){
8 if (/[f, F] red /){
9 chomp;
10 say $ _;
11}
12}
13 #-----------------------------------------------------------#
14 #/fred | Fred/
15 #-----------------------------------------------------------#

Question 3rd: match the row containing the vertex number.

1 #-----------------------------------------------------------#
2 # Source: Learning Perl, chapter7, exercise-3
3 # Date: 2012-01-17
4 # Author: xiaodongrush
5 #-----------------------------------------------------------#
6 use 5.010;
7 while (<> ){
8 if (/\./){
9 chomp;
10 say $ _;
11}
12}
13 #-----------------------------------------------------------#

Question 4th: match the rows starting with an upper-case letter.

1 #-----------------------------------------------------------#
2 # Source: Learning Perl, chapter7, exercise-4
3 # Date: 2012-01-17
4 # Author: xiaodongrush
5 #-----------------------------------------------------------#
6 use 5.010;
7 while (<> ){
8 if (/^ [A-Z]/) {
9 chomp;
10 say $ _;
11}
12}
13 #-----------------------------------------------------------#

Question 5th: contains two consecutive rows with the same non-space characters, such as AA, aa, ¥, etc.

1 #-----------------------------------------------------------#
2 # Source: Learning Perl, chapter7, exercise-5
3 # Date: 2012-01-17
4 # Author: xiaodongrush
5 #-----------------------------------------------------------#
6 use 5.010;
7 while (<> ){
8 if (/(\ S) \ 1 /){
9 chomp;
10 say $ _;
11}
12}
13 #-----------------------------------------------------------#
14 # actually match the BB, aa, 33, --, two identical consecutive characters instead of finding
15 # longest continuous symmetric substring in a string
16 #-----------------------------------------------------------#

Question 6th: Match rows with wilma and fred.

1 #-----------------------------------------------------------#
2 # Source: Learning Perl, chapter7, exercise-6
3 # Date: 2012-01-17
4 # Author: xiaodongrush
5 #-----------------------------------------------------------#
6 use 5.010;
7 while (<> ){
8 if (/wilma. * fred | fred. * wilma /){
9 chomp;
10 say $ _;
11}
12}
13 #-----------------------------------------------------------#
14 # In fact, it is better to use two if statements to determine whether a row exists, fred and wilma, because
15 #. Assume that the row's fred and wilma do not overlap correctly. The two words are not repeated.
The 16 # character. To match abb and bba, abba cannot be
17 #/abb. * bba | bba. * abb/matched, but can be obtained through if (/abb /){
18 # if (/bba/) {}} matches.
19 #-----------------------------------------------------------#

3. File

/Files/pangxiaodong/learns ningperl/ch7-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.