in reply to Breaking Up a Bracketed String

use Set::CrossProduct; my $str = "ATC[TG]CC"; my $strings = []; my @wildcard; $str =~ s{ \[ ([ATCG]+) \] }{ push @wildcard, $1; "%s"; }xgem; # now your string looks like "ATC%sCC" if( @wildcard ) { my @set = map [ split //, $_ ], @wildcard; # now @set contains ( [ 'T', 'G' ] ) # and we weave each possible combination into the %s placeholders +in $str my $xp = Set::CrossProduct->new( \@set ); while( my @tuple = $xp->get ) { push @$strings, sprintf $str, @tuple; } } else { push @$strings, $str; }

The Set::CrossProduct gymnastics are there so that it works properly for strings like "ATC[TG]CCGC[ACTG]" as well. But it's better if, instead of pushing the expanded strings onto @$strings, you process them immediately within the loop. That way, you won’t run into combinatorial memory consumption explosion even if you have heaps and heaps of variable substrings.

Makeshifts last the longest.