my $pattern = join('|', qw(GCGAT CACGT)); $pattern = qr/^(?:$pattern)/; while (<>) { my $line = $_; s/N//; # remove the first N in the string print "match: $line" if $_ =~ $pattern; }