in reply to map function use

my @phrases = <foo bar>; print join "|", map { "\\s$_\\s" } map { quotemeta } @phrases;' __END__ \sfoo\s|\sbar\s

The regex looks fine to me - but without knowing what you match against it's impossible to tell if the regex is appropriate in your case.

Replies are listed 'Best First'.
Re^2: map function use
by newbio (Beadle) on Aug 13, 2009 at 19:21 UTC
    Thanks Moritz. Sure, here is the problem:

    Input sentence: (arf)71 p65(91) H:223 Phospholipase A2 inhibitor ( PLI ) , purified from the blood plasma of the Habu snake ( Trimeresurus flavoviridis ) , was separated into two distinct subunits , PLI-A and PLI-B

    Output tagged sentence: #(arf)71# #p65(91)# #H:223# #Phospholipase A2 inhibitor# ( #PLI# ) , purified from the blood plasma of the Habu snake ( Trimeresurus flavoviridis ) , was separated into two distinct subunits , #PLI-A# and #PLI-B#

    @phrases=('(arf)71', 'p65(91)', 'H:223', 'Phospholipase A2 inhibitor', 'Phospholipase A2', 'Phospholipase', 'PLI-A', 'PLI-B', 'PLI');

      The following works:

      use strict; use warnings; my @phrases=('(arf)71', 'p65(91)', 'H:223', 'Phospholipase A2 inhibito +r', 'Phospholipase A2', 'Phospholipase', 'PLI-A', 'PLI-B', 'PLI'); my $string = join '|', map { quotemeta } @phrases; my $in = "(arf)71 p65(91) H:223 Phospholipase A2 inhibitor ( PLI ) , p +urified from the blood plasma of the Habu snake ( Trimeresurus flavov +iridis ) , was separated into two distinct subunits , PLI-A and PLI-B +"; my $out = "#(arf)71# #p65(91)# #H:223# #Phospholipase A2 inhibitor# ( +#PLI# ) , purified from the blood plasma of the Habu snake ( Trimeres +urus flavoviridis ) , was separated into two distinct subunits , #PLI +-A# and #PLI-B#"; #while($in =~ s/(^|\s)($string)(\s|$)/$1#$2#$3/) {}; while($in =~ s/(^|\s)($string)(\s|$)/$1#$2#$3/g) { pos() = pos() - 1; +}; print "success!!\n" if($in eq $out); print "in: $in\n"; print "out: $out\n";
        pos() = pos() - 1;? ow! Use zero-width matches.
        my $phrases_pat = join '|', map quotemeta, @phrases; $in =~ s/(?<!\S)($phrases_pat)(?!\S)/#$1#/g;

        Bonus: In addition to being much simpler, this should be faster since much less work is outside the substitution, in matching and in replacing.