open SEARCH, $file_to_search or die $!; my $text = do { local $/; }; close SEARCH; my $count = 1; while ( $text =~ /($regex_to_search_for)/sg ) { open FOUND, '>', "found$count.txt" or die $!; print FOUND $1; ++$count; }