開発環境
- OS X Mavericks - Apple(OS)
- BBEdit - Bare Bones Software, Inc., Emacs (Text Editor)
- Perl (プログラミング言語)
初めてのPerl 第6版 (Randal L. Schwartz (著)、brian d foy (著)、Tom Phoenix (著)、近藤 嘉雪 (翻訳)、オライリージャパン)、8章(正規表現によるマッチ)の8.10(練習問題)4.を解いてみる。
その他参考書籍
8.10(練習問題)4.
コード(BBEdit, Emacs)
sample190_4.pl
#!/usr/bin/env perl # use diagnostics; use strict; use warnings; use 5.016; use utf8; binmode STDIN, ':utf8'; binmode STDOUT, ':utf8'; binmode STDERR, ':utf8'; my $label = 'word'; while (<>) { chomp; if (/\b(?<$label>\w*a)\b/) { say "Matched: |$`<$&>$'|"; say "'$label' contains '$+{word}'"; } else { say "No match: |$_|"; } }
入出力結果(Terminal)
$ ./sample190_4.pl temp.txt $ ./sample190_4.pl temp.txt No match: |A| Matched: |<a>| 'word' contains 'a' Matched: |<kamimura>'s blog| 'word' contains 'kamimura' Matched: |http://<sitekamimura>.blogspot.com| 'word' contains 'sitekamimura' No match: |KMI| Matched: |http://www.<mkamimura>.com| 'word' contains 'mkamimura' No match: |frederick| Matched: |<Wilma>| 'word' contains 'Wilma' No match: |Alfred| Matched: |<Wilma>| 'word' contains 'Wilma' No match: |fred flintstone| Matched: |<Wilma>| 'word' contains 'Wilma' No match: |Wilmaerick| Matched: |<AlWilma>| 'word' contains 'AlWilma' Matched: |<Wilma> flintstone| 'word' contains 'Wilma' No match: |Mr. Slate | No match: |Mississippi| No match: |Bamm-Bamm | Matched: |<wilama>| 'word' contains 'wilama' No match: |barney| Matched: |Fred and <Wilma>| 'word' contains 'Wilma' Matched: |<Wilma> and Fred| 'word' contains 'Wilma' Matched: |Fred and <Wilma> | 'word' contains 'Wilma' Matched: |<Wilma> and Fred| 'word' contains 'Wilma' Matched: |Fred&<Wilma>| 'word' contains 'Wilma' No match: |Mrs. Fred Flintstone | No match: |I saw Fred yesterday| No match: |I, Fred!| No match: |Z| Matched: |<llama>| 'word' contains 'llama' No match: |fred| $
0 コメント:
コメントを投稿