Your second question is much easier: Yes. Regular expressions can be built from any string, including those supplied by users. Generally, you should use quotemeta or the \Q and \E markers to make sure the string is free from regular expression meta characters like *, ., and more evil eval-type expressions. In your case, you could also check that the string is a valid nt sequence:
my $string = quotemeta shift;
die "Not a valid nucleotide sequence" if $string =~ /[^AGTC]/;
As for the first question, one way would be to build a regex for each possibility. An example:
my $string = "TGAT";
my @nts = map {
my $tmp = $string;
substr $tmp, $_, 1, '.';
$tmp;
} (0 .. length ($string) -1);
my $groupings = join '|', @nts;
my $sample = "TGATTGGAATGTTAGAT";
while ( $sample =~ /($groupings)/go )
{
print "Matched $1 ending at position ", pos $sample, "\n";
}
@_=qw;
Just another Perl hacker,;
;$_=q=print
"@_"= and eval;