2013年7月29日月曜日

開発環境

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

その他参考書籍

5.

コード(BBEdit)

sample.pl

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

while (<>) {
    chomp;
    if (/(\b\w*a\b)(.{0,5})/) {
        print "Matched: |$`<$&>$'|\n";
        print "\$1 contains '$1'\n";
        print "\$2: $2\n";
    } else {
        print "No match: |$_|\n";
    }
}

入出力結果(Terminal)

$ ./sample.pl < test.txt
No match: |A|
Matched: |<a>|
$1 contains 'a'
$2: 
Matched: |<kamimura's bl>og|
$1 contains 'kamimura'
$2: 's bl
Matched: |http://<sitekamimura.blog>spot.com|
$1 contains 'sitekamimura'
$2: .blog
No match: |KMI|
Matched: |http://www.<mkamimura.com >  |
$1 contains 'mkamimura'
$2: .com 
No match: |Fred|
No match: |FRED|
No match: |fred|
No match: |frederick|
No match: |Alfred|
No match: |fred flintstone|
No match: |Mr. Slate     |
No match: |Mississippi|
No match: |Bamm-Bamm    |
Matched: |<wilama>|
$1 contains 'wilama'
$2: 
No match: |barney|
Matched: |<wilma and >fred|
$1 contains 'wilma'
$2:  and 
Matched: |fred and <wilma>|
$1 contains 'wilma'
$2: 
Matched: |<Wilma and >Fred      |
$1 contains 'Wilma'
$2:  and 
Matched: |Fred and <Wilma>|
$1 contains 'Wilma'
$2: 
Matched: |<wilma&fred>|
$1 contains 'wilma'
$2: &fred
Matched: |Mrs. <Wilma Flin>tstone   |
$1 contains 'Wilma'
$2:  Flin
Matched: |I saw <Wilma yest>erday|
$1 contains 'Wilma'
$2:  yest
Matched: |I, <Wilma!>|
$1 contains 'Wilma'
$2: !
No match: |Z|
Matched: |<llama>|
$1 contains 'llama'
$2: 
$

ちなみにpython3.3の場合。

コード(BBEdit)

sample.py

#!/usr/bin/env python3.3
#-*- coding: utf-8 -*-

import re, sys

name = "word"

for line in sys.stdin:
    line = line.rstrip()
    res = re.search(r"(\b\w*a\b)(.{0,5})", line)
    if res:
        print("Matched : |{0}<{1}>{2}|".format(
            line[0:res.start()], res.group(1) + res.group(2), 
            line[res.end():]))
        print("group(1) contains '{0}'".format(res.group(1)))
        print("group(2): {0}".format(res.group(2)))
    else:
        print("No match: |{0}|".format(line))

入出力結果(Terminal)

$ ./sample.py < test.txt
No match: |A|
Matched : |<a>|
group(1) contains 'a'
group(2): 
Matched : |<kamimura's bl>og|
group(1) contains 'kamimura'
group(2): 's bl
Matched : |http://<sitekamimura.blog>spot.com|
group(1) contains 'sitekamimura'
group(2): .blog
No match: |KMI|
Matched : |http://www.<mkamimura.com>|
group(1) contains 'mkamimura'
group(2): .com
No match: |Fred|
No match: |FRED|
No match: |fred|
No match: |frederick|
No match: |Alfred|
No match: |fred flintstone|
No match: |Mr. Slate|
No match: |Mississippi|
No match: |Bamm-Bamm|
Matched : |<wilama>|
group(1) contains 'wilama'
group(2): 
No match: |barney|
Matched : |<wilma and >fred|
group(1) contains 'wilma'
group(2):  and 
Matched : |fred and <wilma>|
group(1) contains 'wilma'
group(2): 
Matched : |<Wilma and >Fred|
group(1) contains 'Wilma'
group(2):  and 
Matched : |Fred and <Wilma>|
group(1) contains 'Wilma'
group(2): 
Matched : |<wilma&fred>|
group(1) contains 'wilma'
group(2): &fred
Matched : |Mrs. <Wilma Flin>tstone|
group(1) contains 'Wilma'
group(2):  Flin
Matched : |I saw <Wilma yest>erday|
group(1) contains 'Wilma'
group(2):  yest
Matched : |I, <Wilma!>|
group(1) contains 'Wilma'
group(2): !
No match: |Z|
Matched : |<llama>|
group(1) contains 'llama'
group(2): 
$

0 コメント:

コメントを投稿