in reply to Re^4: How to use \W in Regular Expression?
in thread How to use \W in Regular Expression?
...like I've already said: get rid of the \W+ in the substitution string:
$str =~ s/$query\W+/<em>$query<\/em> \W+/ig; ^^^
The regex (i.e. s/$query\W+/) matches "perl: " which is then being substituted with the string "<em>$query</em> ".
Update: on second read it seems you want to keep the ":" (-> "ignore")... in which case just don't match it in the first place, as ikegami said below.
What you may have had in mind could be written as
$str =~ s/$query(\W+)/<em>$query<\/em>$1/ig;
that is, $1 would re-insert into the substitution string whatever the captured part in the regex (\W+) matched (i.e. ": "). But as already said, that's not necessary if you don't include it in the match in the first place.
|
|---|