Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

First off, let me explain what I am trying to accomplish :) I have a string that I want to determine how many times a word appears in. (This must be a case insensitive search) I also need to obtain the count of how many times the word appears in the string, AND I want to modify the string a bit, I want to take the EXACT text that was found as a match, and tag it for HTML bold using and . I have had luck doing this with one minor exception. Note the code below:
#START CODELET $Data = "This is the test text for THIS routine."; $SearchString = "this"; $BoldStart = "<b>"; $BoldEnd = "</b>"; $CountNum -= ($Data =~ s/$searchstring/$BoldStart$searchstring$BoldEnd +/gi); print "$Data - $CountNum\n"; exit; #END CODELET
Note, the output is ALMOST perfect: "this is the test text for this - 2" routine. The output I want is: "This is the test text for THIS - 2" routine. As you can see the text is changed to the searchstring, thus the original caps information is modified. I need to find a way to make the match occur and replace the matched text with itself & the bold tags. Any ideas for an elegant solution, I am not that good yet with the matching functions in perl, thus I need a little help.

Replies are listed 'Best First'.
Re: Matching problems
by dws (Chancellor) on Apr 20, 2002 at 00:04 UTC
    The regexp you're using will get a false hit on thistle.

    Try

    my $search = "this"; my $data = "This is the test text for THIS routine."; my $bs = "<b>"; my $be = "</b>"; my $count = $data =~ s/\b($search)\b/$bs\1$be/gi;
    The backreference (\1) preserves case.


    Update: Kanji is right. Use $1 instead of \1.

      $1 also preserves case, with the added benefit of being -w / use warnings;-friendly!

      my $count = $data =~ s/\b($search)\b/$bs$1$be/gi;

          --k.


      This is working quite well, I appreciate the help. An to the rest of the monks that responded thanks also. Using ALL of your reply's I was able to get a little better grasp on how regexp works. I still have some reading and studying to do, but I have accelerated my learning a bit by reading all the helpful information here. Thanks!
Re: Matching problems
by belg4mit (Prior) on Apr 20, 2002 at 00:05 UTC
    That's because you're replacing it with searchstring. You should either use parantheses to catch the match. Or use zero-width lookahead (and behind for closing tag) to do the matching and bold tag insertion.

    --
    perl -pe "s/\b;([mnst])/'\1/mg"

Re: Matching problems
by Fletch (Bishop) on Apr 20, 2002 at 00:03 UTC

    $1 and some parens. perldoc perlre