my $tbltext = qq(a1a2a3a4b1b2b3b4c1c2c3c4); sub fix_column { my $s = shift; my $nth = shift; my $counter = 0; $s =~ s/()/ ++$counter == $nth #is this the nth cell? ? "${1}newtext" #if yes, add "newtext" : $1 #otherwise, leave it /ge; return $s; } $tbltext =~ s/()(.*?)(<\/tr>)/$1.fix_column($2, 3).$3/ge; print "$tbltext\n"; __END__ a1a2a3newtexta4b1b2b3newtextb4c1c2c3newtextc4