/^(\d)(?!\d*\1)(\d)(?!\d*\2)(\d)(?!\d*\3)\d$/; #### use warnings; use strict; for my $one (0 .. 3) { for my $two (0 .. 3) { for my $three (0 .. 3) { for my $four (0 .. 4) { my $test = "$one$two$three$four"; print "$test\n" if $test=~m/^[0-3]{4}$/ and $test=~m/^(\d)(?!\d*\1)(\d)(?!\d*\2)(\d)(?!\d*\3)\d$/; } } } } #### 0123 0132 0213 0231 0312 0321 1023 1032 1203 1230 1302 1320 2013 2031 2103 2130 2301 2310 3012 3021 3102 3120 3201 3210