#use strict; #use warnings; my ($dirname, $file, %words); opendir(DIR, ".") or die "can't opendir $dirname: $!"; while (defined($file = readdir(DIR))) { if ($file =~ /\.html?/) { open my $F, $file or die "Could not open $file\n"; while (<$F>) { #do stuff s/ (?<= # Look back-group weak # Match the word weak # tag (?: # Non capturing group ){6} # There are 7 input tags, one already matches, so 6 to go. ) # End look-back (?= # Look-ahead-group strong ) # End look-ahead / the text you want to insert /ixg; } close $F; } } closedir(DIR);