foreach my $line (@lines) { if ($line=~ m/.*\.htm/) { $doc_title_temp=substr $line, 10; $doc_title=$doc_title_temp; print "Filename is $doc_title"; last; ############### this stops }; #### use strict; use warnings; my @lines = ("https://www.sec.gov/Archives/edgar/data/831001/000095010323011632/0000950103-23-011632.txt\n", "dp198076_424b2-us2342673.htm\n", "dp198076_exfilingfees.htm\n"); foreach my $line (@lines) { if (my ($doc_title) = $line=~ m/(.*\.htm)/) { print "Filename is $doc_title\n"; last; ############### this stops } } __END__ Filename is dp198076_424b2-us2342673.htm