in reply to map function use

Your resulting regexp, and the code you use to create it, is simpler if you factor out the common \s.
my $string = join '|', map {quotemeta} @phrases; $sentence =~ s/(\s$string\s)/#$1#/g;
But I guess your biggest problem is that the regexp uses $phrases, while the join creates $string.

Replies are listed 'Best First'.
Re^2: map function use
by AnomalousMonk (Archbishop) on Aug 14, 2009 at 05:52 UTC
    ... if you factor out the common \s.
    my $string = join '|', map {quotemeta} @phrases; $sentence =~ s/(\s$string\s)/#$1#/g;
    But if  @phrases was something like  qw(foo bar baz) then the final substitution regex would be equivalent to
        $sentence =~ s/(\sfoo|bar|baz\s)/#$1#/g;
    in which a leading space is associated only with  foo and a trailing space only with  baz and nothing with  bar.

    Adding a non-capturing sub-grouping should do the trick:
        $sentence =~ s/(\s(?:$string)\s)/#$1#/g;