開発環境
- OS X Lion - Apple(OS)
- BBEdit - Bare Bones Software, Inc.(Text Editor)
- Script言語:Perl
その他参考書籍
1.
コード(BBEdit)
sample.pl
#!/usr/bin/env perl
use strict;
use warnings;
use utf8;
use 5.016;
binmode STDOUT, ':utf8';
binmode STDIN, ':utf8';
my $dir = "/etc/";
chdir "/etc" or die "$!";
while(1){
chomp(my $pattern = <STDIN>);
last if $pattern =~ /^\s*$/;
my @matched = map {"$_\n"} grep{ eval{ /$pattern/ } } glob ".* *";
if($@){
warn $@;
} else {
print @matched;
}
}
入出力結果(Terminal)
$ ./sample.pl lo csh.login csh.logout kern_loader.conf localtime locate.rc mach_init_per_login_session.d newsyslog.conf newsyslog.d syslog.conf ^lo localtime locate.rc [ Unmatched [ in regex; marked by <-- HERE in m/[ <-- HERE / at ./sample.pl line 16, <STDIN> line 3. ] ( Unmatched ( in regex; marked by <-- HERE in m/( <-- HERE / at ./sample.pl line 16, <STDIN> line 5. ) Unmatched ) in regex; marked by <-- HERE in m/) <-- HERE / at ./sample.pl line 16, <STDIN> line 6. \( $
pythonの場合。
sample.py
コード(BBEdit)
#!/usr/bin/env python3.3
#-*- coding: utf-8 -*-
import os, re, glob
os.chdir("/etc/")
files = glob.glob("*")
while True:
pattern = input()
if re.search(r"^\s*$", pattern): break
matched = []
try:
for s in files:
if re.search(pattern, s):
matched.append(s)
except Exception as err:
print(err)
else:
print(matched)
入出力結果(Terminal)
$ ./sample.py lo ['csh.login', 'csh.logout', 'kern_loader.conf', 'localtime', 'locate.rc', 'mach_init_per_login_session.d', 'newsyslog.conf', 'newsyslog.d', 'syslog.conf'] ^lo ['localtime', 'locate.rc'] [ unexpected end of regular expression ] [] ( unbalanced parenthesis ) unbalanced parenthesis \( [] $
0 コメント:
コメントを投稿