in reply to Re^3: How to use \W in Regular Expression?
in thread How to use \W in Regular Expression?

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;

Replies are listed 'Best First'.
Re^5: How to use \W in Regular Expression?
by ikegami (Patriarch) on Dec 30, 2009 at 12:37 UTC

    It'll ignore everything it doesn't match. You don't have to do anything.

    If $query contains a literal text to match:

    $str =~ s/\Q$query\E/<em>$query<\/em>/ig;

    If $query contains a regex pattern:

    $str =~ s/($query)/<em>$1<\/em>/ig;
Re^5: How to use \W in Regular Expression?
by almut (Canon) on Dec 30, 2009 at 12:36 UTC

    ...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.

Re^5: How to use \W in Regular Expression?
by AnomalousMonk (Archbishop) on Dec 30, 2009 at 13:10 UTC

    Sachin: Just out of curiosity: when you executed the code fragment in the OP with  -W 'no, really' warnings turned on, didn't you get a warning message?

    >perl -WMstrict -le
    "my $str = 'i am perl: a system';
     my $query = 'perl';
     $str =~ s/$query\W+/<em>$query<\/em> \W+/ig;
     print ' Value = ' . $str;
    "
    Unrecognized escape \W passed through at -e line 1.
     Value = i am <em>perl</em> W+a system

    This should have been a clue.

      He knows it's wrong. He's asking how to fix it.
        Yes, but what I was trying to point out, perhaps rather clumsily, was that the warning message, which Sachin took the trouble to insure would be generated, not only says what's wrong, but also implies how to fix it: the problematic  '\W' is an escape sequence when it's in a substitution string.