2012年12月11日火曜日

開発環境

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

その他参考書籍

5.

コード(TextWrangler)

sample.pl

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

while(<>){
  chomp;
  if(/\b(\w*a)\b(.{0,5})/){
    print "Matched: |$`<$&>$'|\n";
    print "  'word' contains '$1'\n";
    print "   after: '$2'\n";
  } else {
    print "No match: |$_|\n";
  }
}

入出力結果(Terminal)

$ ./sample.pl test.txt
No match: |A|
Matched: |<a>|
  'word' contains 'a'
   after: ''
Matched: |<kamimura's bl>og|
  'word' contains 'kamimura'
   after: ''s bl'
Matched: |http://<sitekamimura.blog>spot.com|
  'word' contains 'sitekamimura'
   after: '.blog'
No match: |KMI|
Matched: |http://www.<mkamimura.com >  |
  'word' contains 'mkamimura'
   after: '.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>|
  'word' contains 'wilama'
   after: ''
No match: |barney|
Matched: |<wilma and >fred|
  'word' contains 'wilma'
   after: ' and '
Matched: |fred and <wilma>|
  'word' contains 'wilma'
   after: ''
Matched: |<Wilma and >Fred      |
  'word' contains 'Wilma'
   after: ' and '
Matched: |Fred and <Wilma>|
  'word' contains 'Wilma'
   after: ''
Matched: |<wilma&fred>|
  'word' contains 'wilma'
   after: '&fred'
Matched: |Mrs. <Wilma Flin>tstone   |
  'word' contains 'Wilma'
   after: ' Flin'
Matched: |I saw <Wilma yest>erday|
  'word' contains 'Wilma'
   after: ' yest'
Matched: |I, <Wilma!>|
  'word' contains 'Wilma'
   after: '!'
No match: |Z|
Matched: |<llama>|
  'word' contains 'llama'
   after: ''
$ 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

ちなみにJavaScriptの場合。

コード(TextWrangler)

var result = "";
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 lines = str.split("\n");
for(var i = 0; i < lines.length; i++){
  if(lines[i].match(/\b(\w*a)\b(.{0,5})/)){
    result += "|" + lines[i] + "|\n" +
      "    '" + RegExp.$1 + "'\n" + 
      "    '" + RegExp.$2 + "'\n";
  }
}
$('#pre0').text(result);


pythonの場合。

sample.py

コード(TextWrangler)

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

import sys, re

for line in open(sys.argv[1]):
    a = re.search(r"\b(\w*a)\b(.{0,5})", line)
    if a:
        print(line, end="")
        print("    '" + a.group(1) + "'")
        print("    '" + a.group(2) + "'")

入出力結果(Terminal)

$ ./sample.py test.txt
a
    'a'
    ''
kamimura's blog
    'kamimura'
    ''s bl'
http://sitekamimura.blogspot.com
    'sitekamimura'
    '.blog'
http://www.mkamimura.com   
    'mkamimura'
    '.com '
wilama
    'wilama'
    ''
wilma and fred
    'wilma'
    ' and '
fred and wilma
    'wilma'
    ''
Wilma and Fred      
    'Wilma'
    ' and '
Fred and Wilma
    'Wilma'
    ''
wilma&fred
    'wilma'
    '&fred'
Mrs. Wilma Flintstone   
    'Wilma'
    ' Flin'
I saw Wilma yesterday
    'Wilma'
    ' yest'
I, Wilma!
    'Wilma'
    '!'
llama
    'llama'
    ''
$

0 コメント:

コメントを投稿