my @in_order = ($str =~ m{ ^(?> .*? ([abcd]) ) (?> .*? (?!\1) ([abcd]) ) (?> .*? (?!\1|\2) ([abcd]) ) (?> .*? (?!\1|\2|\3) ([abcd]) ) }x); #### sub four_or_less { my ($s) = @_ ; $s =~ tr/abcd//cd ; my $c = '' ; $c .= $1 while ($s =~ m/([^ $c])/g) ; return $c ; }