開発環境
- OS X Lion - Apple(OS)
- TextWrangler(Text Editor) (BBEditの機能制限無料版、light版)
- Script言語:Perl
『初めてのPerl 第5版』(Randal L. Schwartz, Tom Phoenix, brian d foy 共著、近藤 嘉雪 訳、オライリー・ジャパン、2009年、ISBN978-4-87311-427-9)の9章(正規表現によるテキスト処理), 9.6(練習問題)6を解いてみる。
用意したファイル、sample
abcdefredfredfred12345 abcdefredfredbarney12345 abcdebarneyfredfred12345 abcdebarneybarneybarney12345 abcde12345 fred barney
1.
やり方の1つ。(「やり方は何通りもある」(TIMTOWTDI(There Is More Than One Way To Do It.)))
コード(TextWrangler)
#!/usr/bin/env perl use strict; use warnings; my $what = 'fred'; while(<>){ chomp; if(/($what){3}/){ print "Matched: |$`<$&>$'|\n"; } else { print "No match: |$_|\n"; } }
入出力結果(Terminal)
$ ./perl_program sample Matched: |abcde<fredfredfred>12345| No match: |abcdefredfredbarney12345| No match: |abcdebarneyfredfred12345| No match: |abcdebarneybarneybarney12345| No match: |abcde12345| No match: |fred| No match: |barney| $
コード(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_program sample Matched: |abcde<fredfredfred>12345| Matched: |abcde<fredfredbarney>12345| Matched: |abcde<barneyfredfred>12345| Matched: |abcde<barneybarneybarney>12345| No match: |abcde12345| No match: |fred| No match: |barney| $
0 コメント:
コメントを投稿