Learning Perl ex8-5
am 27.09.2011 04:57:49 von Chris StinemetzI 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?
Program from previous exercise:
#!/usr/bin/perl
use warnings;
use strict;
# This next line of code is used when you get to Chapter 9.
my $what = 'fred|barney';
while (
chomp;
# If you want to try matching strings which may contain
# newlines, here's the trick to use: Uncomment this next
# line, then use a pound sign ("#") wherever you mean to
# have a newline within your data string.
# s/#/\n/g;
if (/(?
print "Matched: |$`<$&>$'|\n";
print "'word' contains '$+{word}'\n"; # The new output line
# If you need these for testing patterns with
# memories, uncomment them as well
# print " And memory one got <$1>\n";
# print " And memory two got <$2>\n";
} else {
print "No match: |$_|\n";
}
}
excercise hints:
m!
(\b\w*a\b)
(.{0,5})
!xs
Thank you,
Chris
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/