2012年3月12日月曜日

開発環境

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

用意したファイル、test

A
a
kamimura's blog
http://sitekamimura.blogspot.com
KMI
http://www.mkamimura.com   
Fred
FRED
fred
frederick
Alfred
fred flintstone
Mr. Slate     
Mississippi
Bamm-Bamm    
wilama
barney
wilma and fred
fred and wilma
Wilma and Fred      
Fred and Wilma
wilma&fred
Mrs. Wilma Flintstone   
I saw Wilma yesterday
I, Wilma!
Z
llama

3.

やり方の1つ。(「やり方は何通りもある」(TIMTOWTDI(There Is More Than One Way To Do It.)))

コード(TextWrangler)

#!/usr/bin/env perl
use strict;
use warnings;

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

入出力結果(Terminal)

$ ./perl_program test
No match: |A|
No match: |a|
Matched: |<kamimura>'s blog|
$1 contains 'kamimura'
Matched: |<http://sitekamimura>.blogspot.com|
$1 contains 'http://sitekamimura'
No match: |KMI|
Matched: |<http://www.mkamimura>.com   |
$1 contains 'http://www.mkamimura'
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'
No match: |barney|
Matched: |<wilma> and fred|
$1 contains 'wilma'
Matched: |<fred and wilma>|
$1 contains 'fred and wilma'
Matched: |<Wilma> and Fred      |
$1 contains 'Wilma'
Matched: |<Fred and Wilma>|
$1 contains 'Fred and Wilma'
Matched: |<wilma>&fred|
$1 contains 'wilma'
Matched: |<Mrs. Wilma> Flintstone   |
$1 contains 'Mrs. Wilma'
Matched: |<I saw Wilma> yesterday|
$1 contains 'I saw Wilma'
Matched: |<I, Wilma>!|
$1 contains 'I, Wilma'
No match: |Z|
Matched: |<llama>|
$1 contains 'llama'
$

4.

やり方の1つ。(「やり方は何通りもある」(TIMTOWTDI(There Is More Than One Way To Do It.)))

コード(TextWrangler)

#!/usr/bin/env perl
use strict;
use warnings;

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

入出力結果(Terminal)

$ ./perl_program test
No match: |A|
No match: |a|
Matched: |<kamimura>'s blog|
<word> contains 'kamimura'
Matched: |<http://sitekamimura>.blogspot.com|
<word> contains 'http://sitekamimura'
No match: |KMI|
Matched: |<http://www.mkamimura>.com   |
<word> contains 'http://www.mkamimura'
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>|
<word> contains 'wilama'
No match: |barney|
Matched: |<wilma> and fred|
<word> contains 'wilma'
Matched: |<fred and wilma>|
<word> contains 'fred and wilma'
Matched: |<Wilma> and Fred      |
<word> contains 'Wilma'
Matched: |<Fred and Wilma>|
<word> contains 'Fred and Wilma'
Matched: |<wilma>&fred|
<word> contains 'wilma'
Matched: |<Mrs. Wilma> Flintstone   |
<word> contains 'Mrs. Wilma'
Matched: |<I saw Wilma> yesterday|
<word> contains 'I saw Wilma'
Matched: |<I, Wilma>!|
<word> contains 'I, Wilma'
No match: |Z|
Matched: |<llama>|
<word> contains 'llama'
$

0 コメント:

コメントを投稿