At 9:57 PM -0500 9/26/11, Chris Stinemetz wrote:
I am having some difficulty completing this exercise from llama book.

Maybe someone can explain memory variables to me  when working with
regular expressions.

The exercise:

Modify the program from the previous exercise so that immediately
following the word ending in "a" it will also capture up to five
characters (if three are that many characters, of course)
in a separate memory variable. Update 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 just one character. Does your
pattern still match just plain wilma?


"Memory variables" are also called "capture buffers" and "numbered match variables". See 'perldoc perlre' and search for 'Capture buffers':

       "The bracketing construct "( ... )" creates capture buffers. To refer to
       the current contents of a buffer later on, within the same pattern, use
       \1 for the first, \2 for the second, and so on.  Outside the match use
       "$" instead of "\"."

excercise hints:

m!
    (\b\w*a\b)
    (.{0,5})
!xs

That regular expression contains 2 capturing parentheses pairs. Therefore, after a successful match, the matched substrings will be stored in $1 and $2.

--
Jim Gibson
j...@gibson.org

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to