in reply to Regex / "Sophisticated" End of Line

Not sure if I undestand correctly what you try to do, but I assume that you want to extract the words that either start with "NM_" or with "XP_" for all lines that contain "Homo sapiens".

If that is what you want it is easy:

while (my $line=<$fh>) { # assuming a loop through your file here my ($nm_xp) = $line =~ /Homo sapiens.*\s(NM_\w*|XP_\w*)/; next unless $nm_xp; print "$nm_xp for a great ape found at line $.\n"; }