Learning Perl学习笔记(7)第八章 使用正则表达式进行matching

第一题

Using the pattern test program, make a pattern to match the string match. Try
the program with the input string beforematchafter. Does the output show the
three parts of the match in the right order?

脚本:

#!/usr/bin/perl
use warnings;
use strict;
use v5.24;

my $string_word = "beforematchafter";
if ($string_word =~ m/(match)/) {
        print "Matched: |$`<$&>$'|\n"; #分别代表匹配前面的部分,完全匹配的部分,以及匹配部分后面的字符串
} else {
        print "No match: |$_|\n";
}

运行:

$ ./practice1.pl
Matched: |beforeafter|

第二题:

Using the pattern test program, make a pattern that matches if any word (in
the \w sense of word) ends with the letter a. Does it match wilma but not barney?
Does it match Mrs. Wilma Flintstone? What about wilma&fred?

脚本:

#!/usr/bin/perl
use warnings;
use strict;
use v5.24;

while(<>){
        chomp;
        if (m/\wa\b/){ #这里的\w代表匹配word,a\b代表匹配以a为字符边界的字符串
                print "Matched: |$`<$&>$'|\n";
        }else{
                print "No match: |$_|\n";
        }
}

小文本:

$ cat text1.txt
fred
wilma
barney
Alfred
fred and wilma
Mrs. Wilma Flintstone
wilma&fred

运行:

$ ./practice1.pl text1.txt
No match: |fred|
Matched: |wil|
No match: |barney|
No match: |Alfred|
Matched: |fred and wil|
Matched: |Mrs. Wil Flintstone|
Matched: |wil&fred|

第三题

Modify the program from the previous exercise so that the word ending with
the letter a is captured into $1. Update the code to display that variable’s contents in single quotes, something like $1 contains 'Wilma'.

脚本:

#!/usr/bin/perl
use warnings;
use strict;
use v5.24;

while(<>){
        chomp;
        if (m/(\b\w*a\b)/){
                print "Matched: \$1 contains '$1';\n";
        }else{
                print "No match: |$_|\n";
        }
}

运行:

$ ./practice1.pl text1.txt
No match: |fred|
Matched: $1 contains 'wilma';
No match: |barney|
No match: |Alfred|
Matched: $1 contains 'wilma';
Matched: $1 contains 'Wilma';
Matched: $1 contains 'wilma';

第四题

Modify the program from the previous exercise to use named captures
instead of relying on $1. Update the code to display that label name, something like 'word' contains 'Wilma'.

脚本:

#!/usr/bin/perl
use warnings;
use strict;
use v5.24;

while(<>){
        chomp;
        if (m/(?\b\w*a\b)/){
                print "Matched: 'word' contains '$+{name}';\n";
        }else{
                print "No match: |$_|\n";
        }
}

运行:

$ ./practice1.pl text1.txt
No match: |fred|
Matched: 'word' contains 'wilma';
No match: |barney|
No match: |Alfred|
Matched: 'word' contains 'wilma';
Matched: 'word' contains 'Wilma';
Matched: 'word' contains 'wilma';

第五题

Extra credit exercise: modify the program from the previous exercise so that
immediately following the word ending in a it will also capture up-to-five characters (if there are that many characters, of course) in a separate capture variable. Update the code to display both capture variables. For example, if the input string says I saw Wilma yesterday, the up-to-five characters are “ yest” (with the leading space). If the input is I, Wilma!, the extra capture should have just one character. Does your pattern still match just plain wilma?

脚本:

#!/usr/bin/perl
use warnings;
use strict;
use v5.24;

while(){
        chomp;
        if (m/
                (\b\w*a\b)
                (.{0,5}) #这一行是说先匹配一个字符,然后后面跟0-5个字符
        /x){
                print "Matched: 'word' contains '$2';\n"; #这里的$2是指定输出的结果是第二个匹配的pattern
        }
}

运行:

$ ./practice1.pl
I saw Wilma yesterday #输入本行,回车,得到下一行输出结果
Matched: 'word' contains ' yest';
I,Wilma! #输入另外的文本
Matched: 'word' contains '!';

第六题

Write a new program (not the test program!) that prints out any input line
ending with whitespace (other than just a newline). Put a marker character at the end of the output line so as to make the whitespace visible.

脚本:

#!/usr/bin/perl
use warnings;
use strict;
use v5.24;

while(){
        chomp;
        if (m/\w*\s\z/){ #\w代表匹配word,\s代表匹配空格,\z代表匹配字符串结尾
                print "Matched: $_*\n"; #这里的星号用来作为标记,方便看到输出内容里的空格
        }else{
                print "Unmatched\n";
        }
}

运行:

$ ./practice1.pl
space    #如果这里我输入space加一个空格,回车,这时显示的是matched,输出的内容里可以看出单词和星号之间有空格。
Matched: space *
space   #但是如果这里我只输入space,回车后就显示unmatched
Unmatched

你可能感兴趣的:(Learning Perl学习笔记(7)第八章 使用正则表达式进行matching)