use Data::Dumper; my $unqchar5_regex = qr/^(?{%_=()})(?:(.)(?{$_{$1}++}))+ (??{(scalar(keys %_)<5)?~$1:''})$/x; my $pswd = "abcdef"; print (($pswd =~ $unqchar5_regex)?"Pass\n":"Fail\n"); print Dumper \%_; my $pswd = "abcdbd"; print (($pswd =~ $unqchar5_regex)?"Pass\n":"Fail\n"); print Dumper \%_;