in reply to How to use \W in Regular Expression?

The substitution part of s/// is not a regular expresssion, so it doesn't make much sense to put \W+ in there.  Just remove it:

$str =~ s/$query\W+/<em>$query<\/em> /ig;

Replies are listed 'Best First'.
Re^2: How to use \W in Regular Expression?
by Sachin (Acolyte) on Dec 30, 2009 at 12:14 UTC
    So could you tell me how to highlight this word "perl" in above program?

      Not sure what you mean. There's no problem with adding <em>...</em> around the word "perl", it's just the \W+ that's inappropriate in the string to substitute...

      Think of it this way: What concrete characters (and how many) should be substituted from the class of characters that \W stands for?

        I would like to use \W in the following program to ignore special character(:) after word "perl" in above example. and want to highlight only word "perl". So how can i do it?
        #!/usr/bin/perl -W $str = <STDIN>;# suppose $str = "i am perl: a system"; chomp($str); $query = <STDIN>;# suppose $query = 'perl'; chomp($query); #$query = "perl"; $str =~ s/$query\W+/<em>$query<\/em> \W+/ig; print " Value = " . $str;