jordancurve icon

Check that regexp matches everything except MOOSE

jordancurve | PRO | 03/05/20 04:33:52 PM UTC | 0 ⭐ | 8404 👁️ | Never ⏰ | []
Perl |

1.04 KB

|

None

|

0 👍

/

0 👎

#!/usr/bin/perl
# Checks that the given regexp matches everything except the given string.
# Tests with all strings up to 8 characters in length that are composed of the
# characters the string, plus '#', which is assumed not to occur in the string.
 
use strict;
use warnings;
my $string = "MOOSE";
my $regexp = qr/^(.{0,4}$|.{6}|[^M]|.[^O]|..[^O]|...[^S]|....[^E])/;
 
my %char;
for (split //, $string) {
  $char{$_}++;
}
$char{'#'}++;
 
my @chars = sort keys %char;
 
print "@chars\n";
 
sub testit {
  local ($_) = @_;
  my $got = /$regexp/ ? "true" : "false";
  my $want = $_ ne $string ? "true" : "false";
  if ($got ne $want) {
    die "regexp('$_')=$got; want $want\n";
  }
}
 
for my $length (1..8) {
  allStringsOfLength($length, [@chars], \&testit, "")
}
 
print "OK\n";
 
sub allStringsOfLength {
  my ($length, $chars, $func, $prefix) = @_;
  if (length($prefix) == $length) {
    $func->($prefix);
    return;
  }
  foreach my $char (@$chars) {
    allStringsOfLength($length, $chars, $func, $prefix.$char);
  }
}

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎

    
        
  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎