2012年6月8日金曜日

開発環境

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

1.

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

コード(TextWrangler)

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

my $what = 'fred|barney';
while(<>){
  chomp;
  if(/($what){3}/){
    print "Matched: |$`<$&>$'|\n";
  } else {
    print "No match: |$_|\n";
  }
}

入出力結果(Terminal)

$ perl sample.pl
fredfredbarney
Matched: |<fredfredbarney>|
barneybarneybarney
Matched: |<barneybarneybarney>|
barneyfredfred
Matched: |<barneyfredfred>|
$

0 コメント:

コメントを投稿