開発環境
- macOS Sierra - Apple (OS)
- Emacs (Text Editor)
- Perl 6 (プログラミング言語)
- Rakudo(コンパイラ、実装)
Think Perl 6: How to Think Like a Computer Scientist (Laurent Rosenfeld(著)、Allen B. Downey(著)、Oreilly & Associates Inc)のChapter 7(Strings)のExercises on regexesを取り組んでみる。
Exercises on regexes
コード(Emacs)
#!/usr/bin/env perl6
while True {
my $str = $*IN.get;
last if $str eq 'q' ;
say $str;
say ~$/, ': Matched!' if $str ~~ /^\D*\d**10\D*$/;
}
while True {
my $str = $*IN.get;
last if $str eq 'q';
say $str;
say ~$/, ': Matched!' if $str ~~ /^<[0..7]>+$/;
}
while True {
my $str = $*IN.get;
last if $str eq 'q';
say $str;
say ~$0, ': first word' if $str ~~ /^(\w+)/;
}
while True {
my $str = $*IN.get;
last if $str eq 'q';
say $str;
say ~$0, ': first word (a)' if $str ~~ /^(a\w*)/;
}
while True {
my $str = $*IN.get;
last if $str eq 'q';
say $str;
say ~$0, ': first word (lower case vowel)' if $str ~~ /^(<[aeiou]>\w*)/;
}
while True {
my $str = $*IN.get;
last if $str eq 'q';
say $str;
say ~$0, ': French mobile' if $str ~~ /^((06|07)\d**8)$/;
}
while True {
my $str = $*IN.get;
last if $str eq 'q';
say $str;
say ~$0, ': first word (case vowel)' if $str ~~ /^(:i<[aeiou]>\w*)/;
}
while True {
my $str = $*IN.get;
last if $str eq 'q';
say $str;
say ~$/, ': double letter' if $str ~~ /(.)$0/;
}
while True {
my $str = $*IN.get;
last if $str eq 'q';
say $str;
say ~$/[1], ': double letter(second)' if ($str ~~ m:g/(\w)$0/) and $/[1];
}
入出力結果(Terminal)
$ ./regexes.pl < input1.tx 1 123456789 1234567890 1234567890: Matched! 12345678901 0 0: Matched! 7 7: Matched! 8 17 17: Matched! 18 hello, world! hello: first word hello, perl world! hello: first word hello, world! another hello, world! another: first word (a) a b a: first word (a) abcde fghij abcde: first word (lower case vowel) bcde fghij cde fghij de fghij e fghij e: first word (lower case vowel) 123467890 051234678 061234678 071234678 081234678 0512346789 0612346789 0612346789: French mobile 0712346789 0712346789: French mobile 0812346789 abcde fghij abcde: first word (case vowel) bcde fghij cde fghij de fghij e fghij e: first word (case vowel) Abcde fghij Abcde: first word (case vowel) Bcde fghij Cde fghij De fghij E fghij E: first word (case vowel) abcde aabcd aa: double letter aaabcde aa: double letter abbcde bb: double letter abbbcde bb: double letter abccde cc: double letter abcdde dd: double letter abcdee ee: double letter aabcdee aa: double letter abcde aabcd aabbcd bb: double letter(second) aaabbcde bb: double letter(second) aaabbbcde bb: double letter(second) abbcde abbcdde dd: double letter(second) abbcddde dd: double letter(second) abbbcde abbbcdde dd: double letter(second) abbbcddde dd: double letter(second) abccde abcdde abcdee aabcdee ee: double letter(second) $
0 コメント:
コメントを投稿