開発環境
- macOS Mojave - Apple (OS)
- Emacs (Text Editor)
- Windows 10 Pro (OS)
- Strawberry Perl (WindowsのPerlの言語処理系)
- Visual Studio Code (Text Editor)
- Perl 5.28 (プログラミング言語)
初めてのPerl 第7版 (Randal L. Schwartz(著)、brian d foy(著)、Tom Phoenix(著)、近藤 嘉雪(翻訳)、嶋田 健志(翻訳)、オライリージャパン)の8章(正規表現によるマッチ)、8.7(練習問題)4の解答を求めてみる。
コード
#!/usr/bin/env perl
use strict;
use warnings;
use v5.28;
say '4.';
my $label = 'word';
while (<>) {
chomp;
if (/(?<$label>\w+a)\b/) {
say "Matched: |$`<$&>$'|";
say "'$label' contains '$+{$label}'.";
} else {
say "No match: |$_|";
}
}
入出力結果(Zsh、PowerShell、Terminal)
% cat sample.txt
Fred
frederick
Alfred
fred flintstone
Mr. Slate
Fred
Mississippi
Bamm-Bamm
llama
wilma
fred
FRED
wilma fred
fred wilma
wilmafred
WilmaFred
barney
Mrs. Wilma flintstone
I saw Wilma Yesterday
I, Wilma!
% ./sample4.pl sample.txt
4.
No match: |Fred|
No match: |frederick|
No match: |Alfred|
No match: |fred flintstone|
No match: |Mr. Slate|
No match: |Fred|
No match: |Mississippi|
No match: |Bamm-Bamm|
Matched: |<llama>|
'word' contains 'llama'.
Matched: |<wilma>|
'word' contains 'wilma'.
No match: |fred|
No match: |FRED|
Matched: |<wilma> fred|
'word' contains 'wilma'.
Matched: |fred <wilma>|
'word' contains 'wilma'.
No match: |wilmafred|
No match: |WilmaFred|
No match: |barney|
Matched: |Mrs. <Wilma> flintstone|
'word' contains 'Wilma'.
Matched: |I saw <Wilma> Yesterday|
'word' contains 'Wilma'.
Matched: |I, <Wilma>!|
'word' contains 'Wilma'.
%
0 コメント:
コメントを投稿