開発環境
- 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) の8章(正規表現によるパターンマッチ), 8.10(練習問題)5を解いてみる。
5.
やり方の1つ。(「やり方は何通りもある」(TIMTOWTDI(There Is More Than One Way To Do It.)))
コード(TextWrangler)
#!/usr/bin/env perl use strict; use warnings; while(<>){ chomp; if(/(?<word1>\b\w*a\b)(?<word2>.{0,5})/){ print "Matched: |$`<$&>$'|\n"; print "'word1' contain(s) '$+{word1}'\n"; print "直後の最大5文字:$+{word2}\n"; } else { print "No match: |$_|\n"; } }
入出力結果(Terminal)
$ perl sample.pl test No match: |A| Matched: |<a>| 'word1' contain(s) 'a' 直後の最大5文字: Matched: |<kamimura's bl>og| 'word1' contain(s) 'kamimura' 直後の最大5文字:'s bl Matched: |http://<sitekamimura.blog>spot.com| 'word1' contain(s) 'sitekamimura' 直後の最大5文字:.blog No match: |KMI| Matched: |http://www.<mkamimura.com > | 'word1' contain(s) 'mkamimura' 直後の最大5文字:.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>| 'word1' contain(s) 'wilama' 直後の最大5文字: No match: |barney| Matched: |<wilma and >fred| 'word1' contain(s) 'wilma' 直後の最大5文字: and Matched: |fred and <wilma>| 'word1' contain(s) 'wilma' 直後の最大5文字: Matched: |<Wilma and >Fred | 'word1' contain(s) 'Wilma' 直後の最大5文字: and Matched: |Fred and <Wilma>| 'word1' contain(s) 'Wilma' 直後の最大5文字: Matched: |<wilma&fred>| 'word1' contain(s) 'wilma' 直後の最大5文字:&fred Matched: |Mrs. <Wilma Flin>tstone | 'word1' contain(s) 'Wilma' 直後の最大5文字: Flin Matched: |I saw <Wilma yest>erday| 'word1' contain(s) 'Wilma' 直後の最大5文字: yest Matched: |I, <Wilma!>| 'word1' contain(s) 'Wilma' 直後の最大5文字:! No match: |Z| Matched: |<llama>| 'word1' contain(s) 'llama' 直後の最大5文字: $ cat 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 $
0 コメント:
コメントを投稿