One of regular expressions’ most useful features is memorization. To do this, just put parentheses around part of your expression and the result will be memorized:
my($name) = /hello, (\w+)/
In this example, we look in $_ for the word “hello” followed by a comma, space, and a word. Since the word, \w+, has parentheses around it, the part of the string that it matches gets memorized. In this example, we are assigning the return value of the regular expression match to $name. So if $_ contains “hello, world” then $name gets “world” - very convenient.
But parentheses also do other things besides memorize their contents, and this feature can become annoying. Here’s an example. (more…)
If you know how to use regular expressions you will know about special codes like ^, $, and \w which can be used to indicate position or certain classes of characters in the string. But in fact, anything that works in a double-quoted string can be used in a regular expression!
(more…)
Regular expressions are the best way to parse text in Perl. And when combined with the hash data structure, you can easily build an in-memory structure based on data read in from a file.
(more…)
Perl is not a strongly-typed language. A scalar can hold a string or a number or a reference. But sometimes you need to know what it contains, for example if you are communicating with a strongly-typed system like a relational database, or even if you just want to make sure the user entered a number for his age rather than "old enough" or something of that kind.
(more…)
Regular expressions in Perl are "greedy." That means that if you use a * or + operator in a regular expression, it grabs as much of the string as it can. This can be frustrating at times, but it’s useful in other respects. Consider this:
my $ip_addr = "192.168.1.2";
my ($network, $host) = ($ip_addr =~ /(.+).(.+)/);
print "network=$network host=$hostn";
You need a way to know for sure whether $network gets "192.168.1" and $lastpart gets "2" or whether the split is "192" vs. "168.1.2". The decision was made to have it be "greedy" which means that the first + grabs the lion’s share, and the second one gets the leftovers. Put another way, the first one gets as much as possible short of making it impossible to match the string.
(more…)
Copyright © 1995-2007 William R. Ward dba Bay View
Training. All Rights Reserved. “Bay View Training”,
“Bay View Consulting Services”, “Bay View
Software”, the sailboat logo, and the domain name
“bayview.com” are trademarks and/or service marks of
William R. Ward dba Bay View Training. For more information,
contact
webmaster@bayview.com