開発環境
- OS X Lion - Apple(OS)
- BBEdit - Bare Bones Software, Inc.(Text Editor)
- Script言語:Perl
『初めてのPerl 第6版』(Randal L. Schwartz, Tom Phoenix, brian d foy 共著、近藤 嘉雪 訳、オライリー・ジャパン、2012年、ISBN978-4-87311-567-2) の17章(上級テクニック)、17.6(練習問題)1を解いてみる。
その他参考書籍
1.
コード(BBEdit)
sample.pl
#!/usr/bin/env perl use strict; use warnings; use utf8; use 5.016; binmode STDOUT, ':utf8'; binmode STDIN, ':utf8'; my $file_name = "test.txt"; open my $fh, "<", $file_name or die "$!\n"; chomp(my @strings = <$fh>); while(1){ print "パターンを入力: "; chomp(my $pattern = <STDIN>); last if $pattern =~ /^\s*$/; my @matched = eval{ grep /$pattern/, @strings; }; if($@){ print $@ . "\n"; } else { print @matched . "個\n"; print map{"$_\n"} @matched; } }
入出力結果(Terminal)
$ cat test.txt 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 $ ./sample.pl パターンを入力: and 4個 wilma and fred fred and wilma Wilma and Fred Fred and Wilma パターンを入力: [ab Unmatched [ in regex; marked by <-- HERE in m/[ <-- HERE ab/ at ./sample.pl line 18, <STDIN> line 2. パターンを入力: (ab Unmatched ( in regex; marked by <-- HERE in m/( <-- HERE ab/ at ./sample.pl line 18, <STDIN> line 3. パターンを入力: ab] 0個 パターンを入力: ab) Unmatched ) in regex; marked by <-- HERE in m/ab) <-- HERE / at ./sample.pl line 18, <STDIN> line 5. パターンを入力: \(ab 0個 パターンを入力: ^Mr 2個 Mr. Slate Mrs. Wilma Flintstone パターンを入力: $
ちなみにJavaScriptの場合。
コード(BBEdit)
var s = "A\na\nkamimura's blog\nhttp://sitekamimura.blogspot.com\nKMI\n" + "http://www.mkamimura.com \nFred\nFRED\nfred\nfrederick\nAlfred\nfred flintstone\n" + "Mr. Slate \nMississippi\nBamm-Bamm \nwilama\nbarney\nwilma and fred\n" + "fred and wilma\nWilma and Fred \nFred and Wilma\nwilma&fred\n" + "Mrs. Wilma Flintstone \nI saw Wilma yesterday\nI, Wilma!\nZ\nllama\n"; var strs = s.split("\n"); var result; while(true){ var reg = prompt("パッターンを入力(空白で終了)",""); if(/^\s*$/.test(reg)) break; var matched = []; try{ eval("reg = new RegExp(reg);"); for(var i = 0; i < strs.length; i++){ if(reg.test(strs[i])) matched.push(strs[i]) ; } result = matched.length + "個\n" + matched.join("\n"); alert(result); } catch(e){ alert(e); } }
pythonの場合。
sample.py
コード(BBEdit)
#!/usr/bin/env python3.3 #-*- coding: utf-8 -*- import re file_name = 'test.txt' lines = [] with open(file_name) as f: for line in f: lines.append(line) while True: try: pattern = input("パッターンを入力: ") if re.search(r"^\s*$", pattern): break pattern = eval("re.compile(pattern)") matched = [] for line in lines: if re.search(pattern, line): matched.append(line) print("{0}個".format(len(matched))) for s in matched: print(s, end="") except: import sys print(sys.exc_info()[1])
入出力結果(Terminal)
$ ./sample.py パッターンを入力: and 4個 wilma and fred fred and wilma Wilma and Fred Fred and Wilma パッターンを入力: [ab unexpected end of regular expression パッターンを入力: (ab unbalanced parenthesis パッターンを入力: ab] 0個 パッターンを入力: ab) unbalanced parenthesis パッターンを入力: \(ab 0個 パッターンを入力: ^Mr 2個 Mr. Slate Mrs. Wilma Flintstone パッターンを入力: $
0 コメント:
コメントを投稿