$line= <This is dim.<30>EThis is reversed.<28>This is bold.<27>" END my %fix= ( "<29>" => '@(-38)', "<28>" => '@(-40)', "<27>" => '@(-35)' ); $line =~ s{("?)(<\d+>)("?)}{ if( "<30>" eq $2 ) { "$1$2$3" } else { my $str= $1 ? "" : '";'; $str .= $fix{$2}; $str .= $3 ? "" : ';"'; $str; } }gex; print $line;