第一題
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";
}
運(yùn)行:
$ ./practice1.pl
Matched: |before<match>after|
第二題:
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
運(yùn)行:
$ ./practice1.pl text1.txt
No match: |fred|
Matched: |wil<ma>|
No match: |barney|
No match: |Alfred|
Matched: |fred and wil<ma>|
Matched: |Mrs. Wil<ma> Flintstone|
Matched: |wil<ma>&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";
}
}
運(yù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/(?<name>\b\w*a\b)/){
print "Matched: 'word' contains '$+{name}';\n";
}else{
print "No match: |$_|\n";
}
}
運(yù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(<STDIN>){
chomp;
if (m/
(\b\w*a\b)
(.{0,5}) #這一行是說先匹配一個(gè)字符考蕾,然后后面跟0-5個(gè)字符
/x){
print "Matched: 'word' contains '$2';\n"; #這里的$2是指定輸出的結(jié)果是第二個(gè)匹配的pattern
}
}
運(yùn)行:
$ ./practice1.pl
I saw Wilma yesterday #輸入本行社证,回車,得到下一行輸出結(jié)果
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(<STDIN>){
chomp;
if (m/\w*\s\z/){ #\w代表匹配word,\s代表匹配空格色罚,\z代表匹配字符串結(jié)尾
print "Matched: $_*\n"; #這里的星號(hào)用來作為標(biāo)記,方便看到輸出內(nèi)容里的空格
}else{
print "Unmatched\n";
}
}
運(yùn)行:
$ ./practice1.pl
space #如果這里我輸入space加一個(gè)空格账劲,回車戳护,這時(shí)顯示的是matched,輸出的內(nèi)容里可以看出單詞和星號(hào)之間有空格瀑焦。
Matched: space *
space #但是如果這里我只輸入space腌且,回車后就顯示unmatched
Unmatched