not-a-monk has asked for the wisdom of the Perl Monks concerning the following question:

I seek to color or bold all appearances of the same sequence in a scalar, similar to how grep does in it's results. I can't think of how to do it with Term::ANSIColor but maybe I'm just not creative?
#example; not an attempt at a solution my $word == 'bob'; my $sentence == 'bob said hi to bob'; print "$sentence\n";
The two occurrences of bob in $sentence should be bold. I get my values of $word and $sentence dynamically so no hard-coding.

Replies are listed 'Best First'.
Re: bold matching text
by toolic (Bishop) on Aug 30, 2011 at 19:42 UTC
    s///ge works for me
    use warnings; use strict; use Term::ANSIColor; my $word = 'bob'; my $sentence = 'bob said hi to bob'; print "$sentence\n"; $sentence =~ s/($word)/colored($1, 'bold')/ge; print "$sentence\n"; print color('reset'); print "done\n";