while ($str =~ /]*>\s*\s*([^<]*)<\/endtag3>/gsi) { ++$a; $b = $1; $b = $b . " [ $c-$a]"; $str =~ s/(]*>\s*\s*)[^<]*<\/endtag3>/$1$b<\/endtag3>/si; }