my @tests = qw( 0 1 01 10 00 11 000 111 001 011 101 010 100 110 1001 1101 0101 1010 ); foreach (@tests) { print "$_ "; if (/^(0|1)$|^1(0|1)*1$|^0(0|1)*0$/) { print "passed\n"; } else { print "failed\n"; } }