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