in reply to pattern matching once

To just get the first occurrence, stop searching after the first one is seen.
foreach my $line (@lines) { if ($line=~ m/<FILENAME>.*\.htm/) { $doc_title_temp=substr $line, 10; $doc_title=$doc_title_temp; print "Filename is $doc_title"; last; ############### this stops };
With small re-write:
use strict; use warnings; my @lines = ("https://www.sec.gov/Archives/edgar/data/831001/000095010 +323011632/0000950103-23-011632.txt\n", "<FILENAME>dp198076_424b2-us2342673.htm\n", "<FILENAME>dp198076_exfilingfees.htm\n"); foreach my $line (@lines) { if (my ($doc_title) = $line=~ m/<FILENAME>(.*\.htm)/) { print "Filename is $doc_title\n"; last; ############### this stops } } __END__ Filename is dp198076_424b2-us2342673.htm