2017年5月4日木曜日

開発環境

Think Perl 6: How to Think Like a Computer Scientist (Laurent Rosenfeld(著)、Allen B. Downey(著)、Oreilly & Associates Inc)のPart 1(Starting with the basics)、Chapter 7(Strings)、Building, your Regex Patterns の Exercises on Regexes.を取り組んでみる。

Exercises on Regexes.

コード(Emacs)

#!/usr/bin/env perl6
# -*- coding: utf-8 -*-

say '10 digits';
my $regex = rx/\d ** 10/;
say ~$/ if 'a1234567890bc'~~ $regex;
say ~$/ if 'a123456789bc' ~~ $regex;

say 'valid octal number';
$regex = rx/^<[0..7]>+$/;
say ~$/ if ''~~ $regex;
say ~$/ if '0' ~~ $regex;
say ~$/ if '01234567' ~~ $regex;
say ~$/ if '012345678' ~~ $regex;

say 'first word';
$regex = rx/\w+/;
say ~$/ if 'think perl 6' ~~ $regex;
say ~$/ if 'think' ~~ $regex;
say ~$/ if '日本 東京' ~~ $regex;

say 'first word(starting with an "a")';
$regex = rx/<<a\w*/;
say ~$/ if 'b' ~~ $regex;
say ~$/ if 'like alike' ~~ $regex;
say ~$/ if 'alike like' ~~ $regex;

say 'first word(starting with a lowercase vowel)';
$regex = rx/<<<[aeiou]>\w*/;
say ~$/ if 'b' ~~ $regex;
say ~$/ if 'e' ~~ $regex;
say ~$/ if 'a e' ~~ $regex;
say ~$/ if 'b e' ~~ $regex;
say ~$/ if 'epub' ~~ $regex;
say ~$/ if 'book epub' ~~ $regex;

say 'French mobile telephone nuber.';
$regex = rx:s/^0<[67]>\d**8$/;
say ~$/ if '0612345678' ~~ $regex;
say ~$/ if '061234567' ~~ $regex;
say ~$/ if '06123456789' ~~ $regex;

say 'first word(starting with a vowel)';
$regex = rx/<<:i<[aeiou]>\w*/;
say ~$/ if 'b' ~~ $regex;
say ~$/ if 'e' ~~ $regex;
say ~$/ if 'a e' ~~ $regex;
say ~$/ if 'b e' ~~ $regex;
say ~$/ if 'epub' ~~ $regex;
say ~$/ if 'book epub' ~~ $regex;

say ~$/ if 'B' ~~ $regex;
say ~$/ if 'E' ~~ $regex;
say ~$/ if 'A E' ~~ $regex;
say ~$/ if 'B E' ~~ $regex;
say ~$/ if 'Epub' ~~ $regex;
say ~$/ if 'BOOK EPUB' ~~ $regex;

say 'second occurence of a double letter';
$regex = rx/(\w)$0.*((\w)$0)/;
say ~$1 if 'aa bb' ~~ $regex;
say ~$1 if 'aa bbb' ~~ $regex;
say ~$1 if 'aaa bbb' ~~ $regex;
say ~$1 if 'aa b' ~~ $regex;
say ~$1 if 'ああ いい' ~~ $regex;
say ~$1 if 'ああ いいい' ~~ $regex;
say ~$1 if 'あああ いいい' ~~ $regex;
say ~$1 if 'ああ い' ~~ $regex;

入出力結果(Terminal, REPL)

$ ./sample_regexes.pl
10 digits
1234567890
valid octal number
0
01234567
first word
think
think
日本
first word(starting with an "a")
alike
alike
first word(starting with a lowercase vowel)
e
a
e
epub
epub
French mobile telephone nuber.
0612345678
first word(starting with a vowel)
e
a
e
epub
epub
E
A
E
Epub
EPUB
second occurence of a double letter
bb
bb
bb
いい
いい
いい
$

0 コメント:

コメントを投稿