use strict; use warnings; my $text="12qw34"; $text =~ s/((?:(?!).)*)3((?:(?!<\/td>).)*)/$1a$2/; print "$text"; #### 12qwa4