開発環境
- OS X Lion - Apple(OS)
- TextWrangler(Text Editor) (BBEditの機能制限無料版、light版)
- Script言語:Perl
『初めてのPerl 第6版』(Randal L. Schwartz, Tom Phoenix, brian d foy 共著、近藤 嘉雪 訳、オライリー・ジャパン、2012年、ISBN978-4-87311-567-2) の9章(正規表現によるテキスト処理)、9.6(練習問題)3を解いてみる。
その他参考書籍
3.
コード(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>){
# プレースホルダーとして塗る文字「\0」を使用
s/Fred/\0/gi;
s/Wilma/Fred/gi;
s/\0/Wilma/gi;
print $out_fh $_;
}
入出力結果(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 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 Wilma Wilma Wilma Wilmaerick AlWilma Wilma flintstone Mr. Slate Mississippi Bamm-Bamm wilama barney Fred and Wilma Wilma and Fred Fred and Wilma Wilma and Fred Fred&Wilma Mrs. Fred Flintstone I saw Fred yesterday I, Fred! 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, "\0");
result = result.replace(/wilma/gi, "Fred");
result = result.replace(/\0/gi, "Wilma");
$('#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:
text = in_f.read()
text = re.sub("Fred", "\0", text, flags=re.I)
text = re.sub("Wilma", "Fred", text, flags=re.I)
text = re.sub("\0", "Wilma", text, flags=re.I)
with open(out_name, "w") as out_f:
out_f.write(text)
入出力結果(Terminal)
$ ./sample.py test.txt $ cat test.out A a kamimura's blog http://sitekamimura.blogspot.com KMI http://www.mkamimura.com Wilma Wilma Wilma Wilmaerick AlWilma Wilma flintstone Mr. Slate Mississippi Bamm-Bamm wilama barney Fred and Wilma Wilma and Fred Fred and Wilma Wilma and Fred Fred&Wilma Mrs. Fred Flintstone I saw Fred yesterday I, Fred! Z llama $
0 コメント:
コメントを投稿