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.

In reply to Matching problems by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.