The problem has nothing to do with map.
First of all, you store the pattern in $string, but you use $phrase in the substitution. I think the code you posted isn't the code you actually used.
The first part of the problem you mention is that you want to match the space between "foo" and "bar" in " foo bar" twice. If that was it, you could use (?=) and/or (?<=) to solve the problem.
However, you also want to replace that space twice. I don't see how you can do that entirely in one substitution.
One simple solution is to do a prep pass to double up the space:
my @phrases = qw( foo bar ); my $sentence = " foo bar "; my ($phrases_pat) = map qr/$_/, join '|', map quotemeta, @phrases; $sentence =~ s/($phrases_pat)(\s)($phrases_pat)/$1$2$2$3/g; $sentence =~ s/(\s$phrases_pat\s)/#$1#/g; print("$sentence\n"); # "# foo ## bar #"
Or maybe you want
... $sentence =~ s/($phrases_pat)(\s)($phrases_pat)/$1$2$2$3/g; $sentence =~ s/\s($phrases_pat)\s/#$1#/g; print("$sentence\n"); # "#foo##bar#"
If you consider the start and end of the string to be equivalent to spaces, you'll need to handle those specially.
In reply to Re: map function use
by ikegami
in thread map function use
by newbio
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |