2012年6月3日日曜日

開発環境

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

2.

やり方の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";
  } else {
    print "No match: |$_|\n";
  }
}

入出力結果(Terminal)

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

0 コメント:

コメントを投稿