There's a lot going on in your code; both as a debugging exercise and a convenience to those who would like to help you, you should try to simplify your posts down to the most compact code that replicates your issue. It has been my experience that bugs often identify themselves when I try to isolate them. See How do I post a question effectively?.

Your variable $string contains the escaped contents of the variable $test, which is never initialized in your code. I'm thinking this is an error in your post, not if your original code. Please make sure that posted code replicates what you are actually running, so we can debug the right thing. In this case, both strict and warnings would have flagged the issue. See Use strict warnings and diagnostics or die.

If I'm reading your spec right, your line featuring my $content =~ ... is supposed to grab the entire 'word' following your match. However, because you've used the binding operator (=~), you are actually running your regular expression against an uninitialized value (again, an issue caught by warnings). What you probably meant to do was

my ($content) = m/($string[a-z]*)/;

This runs the previous expression extended to include all trailing lower-case alphabetic characters, extracts the match, and uses list assignment to store the match (List value constructors). Easier would be to run the code as

if (/$string/) { m/($string[a-z]*)/; print "$1\n"; print "found string -$string- in line $num +\n"; print "<br>"; }
since capturing stores the value in $1 (Variables related to regular expressions). Note that I've removed the g modifier and escaped slashes because I don't think that's what you meant.

Let me know if I've missed the spec here.


#11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.


In reply to Re: Big problem in pattern matching by kennethk
in thread Pattern Matching problem by auhakim

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.