Advertisement
jordancurve

Check that regexp matches everything except MOOSE

Mar 5th, 2020
949
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 1.04 KB | None | 0 0
  1. #!/usr/bin/perl
  2. # Checks that the given regexp matches everything except the given string.
  3. # Tests with all strings up to 8 characters in length that are composed of the
  4. # characters the string, plus '#', which is assumed not to occur in the string.
  5.  
  6. use strict;
  7. use warnings;
  8. my $string = "MOOSE";
  9. my $regexp = qr/^(.{0,4}$|.{6}|[^M]|.[^O]|..[^O]|...[^S]|....[^E])/;
  10.  
  11. my %char;
  12. for (split //, $string) {
  13.   $char{$_}++;
  14. }
  15. $char{'#'}++;
  16.  
  17. my @chars = sort keys %char;
  18.  
  19. print "@chars\n";
  20.  
  21. sub testit {
  22.   local ($_) = @_;
  23.   my $got = /$regexp/ ? "true" : "false";
  24.   my $want = $_ ne $string ? "true" : "false";
  25.   if ($got ne $want) {
  26.     die "regexp('$_')=$got; want $want\n";
  27.   }
  28. }
  29.  
  30. for my $length (1..8) {
  31.   allStringsOfLength($length, [@chars], \&testit, "")
  32. }
  33.  
  34. print "OK\n";
  35.  
  36. sub allStringsOfLength {
  37.   my ($length, $chars, $func, $prefix) = @_;
  38.   if (length($prefix) == $length) {
  39.     $func->($prefix);
  40.     return;
  41.   }
  42.   foreach my $char (@$chars) {
  43.     allStringsOfLength($length, $chars, $func, $prefix.$char);
  44.   }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement