in reply to Re^3: search and print in perl
in thread search and print in perl
I have added some non capturing parentheses (the (?: ... ) around the TGA|TAG|TAA) and most important made the + quantifiers non-greedy by adding a ? as otherwise they would match too much and only return you the last gene in your string.</c>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"; }
CountZero
A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James
|
|---|