use strict; use warnings; my $filename = 'input.txt'; open (my $IN, '<', $filename) or die "Can't open file $filename : $! "; my $text; # no need to initialize it to the empty string! while($line = <$IN>) { chomp $line; ## <---- !!! $text .= $line; } while($text =~ m/TATAAT[ACGT]+?(ATG[ACGT]+?(?:TGA|TAG|TAA))/g) { print "$1\n"; }