Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Is there any way to perform a substitution regex that searches for any element of an array and places html bold tags around it? I'm thinking of something like this: $_ =~ s/(\b(@SearchTexts)\b)/\<b\>\1\<\/b\>/iog Thanks in advance.

Replies are listed 'Best First'.
Re: Regex of array
by JavaFan (Canon) on Aug 12, 2009 at 01:17 UTC
    my $clauses = join "|", map {quotemeta} @SearchTexts; s{\b($clauses)\b}{<b>$1</b>}ig;
    • No need for the double matching.
    • Use different delimiters if you have a slash in your replacement.
    • No need to escape < or >.
    • Don't use /o unless you're an expert (and then you won't).
    • Don't use \1 in the replacement. That's what $1 is for.
    • Eat more spinach.
Re: Regex of array
by Anonymous Monk on Aug 12, 2009 at 01:14 UTC
Re: Regex of array
by Anonymous Monk on Aug 12, 2009 at 02:24 UTC
    Thanks guys! Excellent advice.