#!/bin/perl5 use strict; use warnings; use HTML::TokeParser; my $str = q| highlight the word |; my $tp = HTML::TokeParser->new(\$str) or die "Couldn't parse $str: $!"; $tp->unbroken_text(1); my ($html, $start); while (my $tag = $tp->get_token) { if ($tag->[0] eq 'T'){ my $t = $tag->[1]; $t =~ s|word|WORD|g; $html .= $t; } else{ $html .= $tag->[4] if $tag->[0] eq 'S'; $html .= $tag->[1] if $tag->[0] eq 'C'; $html .= $tag->[2] if $tag->[0] eq 'E'; } } print "$html\n"; __DATA__ ---------- Capture Output ---------- > "C:\Perl\bin\perl.exe" parse_str.pl highlight the WORD > Terminated with exit code 0.