use strict; use warnings; use Data::Dumper; my %replace = ( R => '{A,G}', S => '{C,G}', K => '{G,T}', ' ' => '_', ); my $s = 'KAG GTR CAG CTG AAG SAG TCA GG'; my @results; for my $base (keys %replace) { $s =~ s/$base/$replace{$base}/g; } push @results, $_ while glob $s; @results = map { s/_/ /g; $_ } @results;