Hello Monks,

I am in the Llama chapter 8 and I am learning about quantifiers. I am a novice. I am trying to answer the extra credit exercise #4 in chapter 8.

The question states:

Modify the program from the previous exercise so that immediately following the word ending in 'a' it will capture up to five characters (if there are that many characters) in a separate memory variable. Updte the code to display both memory variables. For example, if the input string says "I saw Wilma yesterday, the up to five characters are _yest. If the input is I, Wilma!, the extra memory should have one character. Does your pattern still match just plain wilma?

Here is my answer:

--------------

#!/usr/bin/perl -w use strict; while(<>) { chomp; if (/(\w+a\b)(\w{0,5})/) { print "Matched: |$`<'$1'>'$2'\n"; } else { print "No match: |$_|\n"; } }
-------------

Here is my sample text that I am working with: 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


What I expect to be in the memory variable $2 always turns up empty. Help a brutha out?

In reply to 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.