Here are a few general guidelines which you may find useful (followed by code which incorporates them):

So, using your data (and adding the "... saw Wilma yesterday" part of the exercise statement):

while(<DATA>) { chomp; if ( /(Wilma)([\w\W]{5})/i ) { print "Matched: |<$1> <$2>\n"; } else { print "No match: |$_|\n"; } } __DATA__ This is a line that has the name fred in it. This is a line that does not. This is a line that has the name barney in it. This is fred. This is notfred. Hello Fred Flintstone! Hello Fred! Allo allo, Alfred! Cheerio Mr. frederick! Hello Mr. Slate! Hello FRED! Hello Fred! Hello fred! This is some wilma text with some words in it this is fred. This is wilmafred. This is some more wilma text fred. Mrs. Wilma Flintstone wilma&fred wilma but not barney I saw Wilma yesterday.

Output:

perl 732070.pl No match: |This is a line that has the name fred in it.| No match: |This is a line that does not.| No match: |This is a line that has the name barney in it.| No match: |This is fred.| No match: |This is notfred.| No match: |Hello Fred Flintstone!| No match: |Hello Fred!| No match: |Allo allo, Alfred!| No match: |Cheerio Mr. frederick!| No match: |Hello Mr. Slate!| No match: |Hello FRED!| No match: |Hello Fred!| No match: |Hello fred!| Matched: |<wilma> < text> Matched: |<wilma> <fred.> Matched: |<wilma> < text> Matched: |<Wilma> < Flin> Matched: |<wilma> <&fred> Matched: |<wilma> < but > Matched: |<Wilma> < yest>

Caveat: Neither my Llama nor my Camel edition has the exercise you describe, so this may miss a requirement that [Ww]ilma be followed by a space or by a punctuation mark or symbol, which would substantially change the regex required.

Update: ...and, oh yes, I've simplified your print "Matched.... as well, again in the pursuit of KISS.


In reply to Re: Contents of $2 empty by ww
in thread Contents of $2 empty by bluethundr

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.