2014年3月30日日曜日

開発環境

初めてのPerl 第6版 (Randal L. Schwartz (著)、brian d foy (著)、Tom Phoenix (著)、近藤 嘉雪 (翻訳)、オライリージャパン)、8章(正規表現によるマッチ)の8.10(練習問題)5.を解いてみる。

その他参考書籍

8.10(練習問題)5.

コード(BBEdit, Emacs)

sample190_5.pl

#!/usr/bin/env perl
# use diagnostics;
use strict;
use warnings;
use 5.016;
use utf8;
binmode STDIN, ':utf8';
binmode STDOUT, ':utf8';
binmode STDERR, ':utf8';

my $label = 'word';
while (<>) {
    chomp;
    if (/\b(?<$label>\w*a)\b(?.{0,5})/) {
        say "Matched: |$`<$&>$'|";
        say "'$label' contains '$+{word}'";
        say "after: '$+{after}'";
    } else {
        say "No match: |$_|";
    }
}

入出力結果(Terminal)

$ ./sample190_5.pl temp.txt
No match: |A|
Matched: |<a>|
'word' contains 'a'
after: ''
Matched: |<kamimura's bl>og|
'word' contains 'kamimura'
after: ''s bl'
Matched: |http://<sitekamimura.blog>spot.com|
'word' contains 'sitekamimura'
after: '.blog'
No match: |KMI|
Matched: |http://www.<mkamimura.com>|
'word' contains 'mkamimura'
after: '.com'
No match: |frederick|
Matched: |<Wilma>|
'word' contains 'Wilma'
after: ''
No match: |Alfred|
Matched: |<Wilma>|
'word' contains 'Wilma'
after: ''
No match: |fred flintstone|
Matched: |<Wilma>|
'word' contains 'Wilma'
after: ''
No match: |Wilmaerick|
Matched: |<AlWilma>|
'word' contains 'AlWilma'
after: ''
Matched: |<Wilma flin>tstone|
'word' contains 'Wilma'
after: ' flin'
No match: |Mr. Slate     |
No match: |Mississippi|
No match: |Bamm-Bamm    |
Matched: |<wilama>|
'word' contains 'wilama'
after: ''
No match: |barney|
Matched: |Fred and <Wilma>|
'word' contains 'Wilma'
after: ''
Matched: |<Wilma and >Fred|
'word' contains 'Wilma'
after: ' and '
Matched: |Fred and <Wilma     > |
'word' contains 'Wilma'
after: '     '
Matched: |<Wilma and >Fred|
'word' contains 'Wilma'
after: ' and '
Matched: |Fred&<Wilma>|
'word' contains 'Wilma'
after: ''
No match: |Mrs. Fred Flintstone   |
No match: |I saw Fred yesterday|
No match: |I, Fred!|
No match: |Z|
Matched: |<llama>|
'word' contains 'llama'
after: ''
No match: |fred|
$

0 コメント:

コメントを投稿