開発環境
- 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(練習問題)2の解答を求めてみる。
コード
#!/usr/bin/env perl
use strict;
use warnings;
use utf8;
use autodie;
use v5.18;
# Wide character in print at ... line ...が出力されないようにに指定
# 付録C(Unicode入門)、C.4(ファンシーな文字)、C.4.2(さらにファンシーな文字)より
# WindowsとmacOSのどちらでも標準入力、標準出力、標準エラーについてちゃんと動くように設定
use Encode::Locale;
binmode STDIN, ':encoding(console_in)';
binmode STDOUT, ':encoding(console_out)';
binmode STDERR, ':encoding(console_out)';
say '2.';
while (<>) {
    chomp;
    if (/\w*a\b/) {
        print "Matched: |$`<$&>$'|\n";
    } else {
        print "No match: |$_|\n";
    }
}
入出力結果(cmd(コマンドプロンプト)、Terminal)
C:\Users\...>type input2.txt fred fred wilma Mississippi wilma fred Bamm-Bamm Fred frederick Alfred fred flintstone perl Mr. Slate .emacs . abcde. llama fredwilma wilmafred frea llama wilma llama wilma fred wilma fred llama wilma llama fred llama fred wilma fred wilma llama C:\Users\...>perl sample2.pl < input2.txt 2. No match: |fred| Matched: |fred <wilma>| No match: |Mississippi| Matched: |<wilma> fred| No match: |Bamm-Bamm| No match: |Fred| No match: |frederick| No match: |Alfred| No match: |fred flintstone| No match: |perl| No match: |Mr. Slate| No match: |.emacs| No match: |.| No match: |abcde.| Matched: |<llama>| Matched: |<fredwilma>| No match: |wilmafred| Matched: |<frea> llama wilma| Matched: |<llama> wilma fred| Matched: |<wilma> fred llama| Matched: |<wilma> llama fred| Matched: |<llama> fred wilma| Matched: |fred <wilma> llama| C:\Users\...>
0 コメント:
コメントを投稿