2012年12月14日金曜日

開発環境

『初めてのPerl 第6版』(Randal L. Schwartz, Tom Phoenix, brian d foy 共著、近藤 嘉雪 訳、オライリー・ジャパン、2012年、ISBN978-4-87311-567-2) の9章(正規表現によるテキスト処理)、9.6(練習問題)2を解いてみる。

その他参考書籍

2.

コード(TextWrangler)

sample.pl

#!/usr/bin/env perl
use strict;
use warnings;
use utf8;
use 5.016;
binmode STDIN, ':utf8';
binmode STDOUT, ':utf8';

my $in = $ARGV[0];
die unless defined $in;
my $out = $in;
$out =~ s/(\.\w+)$/.out/;
open my $in_fh, '<', $in or die;
open my $out_fh, '>', $out or die;
while(<$in_fh>){
  s/Fred/Larry/gi;
  print $out_fh $_;
}

入出力結果(Terminal)

kamimura$ 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 test.txt
$ 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
$ cat test.out
A
a
kamimura's blog
http://sitekamimura.blogspot.com
KMI
http://www.mkamimura.com   
Larry
Larry
Larry
Larryerick
AlLarry
Larry flintstone
Mr. Slate     
Mississippi
Bamm-Bamm    
wilama
barney
wilma and Larry
Larry and wilma
Wilma and Larry      
Larry and Wilma
wilma&Larry
Mrs. Wilma Flintstone   
I saw Wilma yesterday
I, Wilma!
Z
llama
$

ちなみにJavaScriptの場合。

コード(TextWrangler)

var str = "A\na\nkamimura's blog\nhttp://sitekamimura.blogspot.com\nKMI\nhttp://www.mkamimura.com   \nFred\nFRED\nfred\nfrederick\nAlfred\nfred flintstone\nMr. Slate     \nMississippi\nBamm-Bamm    \nwilama\nbarney\nwilma and fred\nfred and wilma\nWilma and Fred      \nFred and Wilma\nwilma&fred\nMrs. Wilma Flintstone   \nI saw Wilma yesterday\nI, Wilma!\nZ\nllama\n"
var result = str.replace(/fred/gi, "Larry");
$('#pre0').text(result);


pythonの場合。

sample.py

コード(TextWrangler)

#!/usr/bin/env python3.3
#-*- coding:utf-8 -*-

import sys, re

in_name = sys.argv[1]
out_name = re.sub(r"(\.\w+)?$", ".out", in_name)
with open(in_name) as in_f:
    with open(out_name, "w") as out_f:
        out_f.write(re.sub("fred", "Larry", in_f.read(), flags=re.I))

入出力結果(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
$ ls test.out
ls: test.out: No such file or directory
$ ./sample.py test.txt
$ 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
$ cat test.out
A
a
kamimura's blog
http://sitekamimura.blogspot.com
KMI
http://www.mkamimura.com   
Larry
Larry
Larry
Larryerick
AlLarry
Larry flintstone
Mr. Slate     
Mississippi
Bamm-Bamm    
wilama
barney
wilma and Larry
Larry and wilma
Wilma and Larry      
Larry and Wilma
wilma&Larry
Mrs. Wilma Flintstone   
I saw Wilma yesterday
I, Wilma!
Z
llama
$

0 コメント:

コメントを投稿